This program works identically to the one you just wrote, but it has been organized differently because some functions return a value instead of logging them to the console.

Do This

Read the code for this program
Finish the code for the randomCompliment function. Use the comments and the code for randomGreeting to help.

greetAndCompliment("Benjamin");
greetAndCompliment("Raji");
greetAndCompliment("Esmerelda");

function greetAndCompliment(name){
console.log(randomGreeting() + name);
console.log(name + randomCompliment());
}

// Generates a random greeting
// return {string} - random greeting, followed by a comma and space
function randomGreeting(){

var greetings = ["Good day, ", "Bonne journée, ", "Buenos dias, ", "Magandang araw, ", "美好的一天, "];
var randomGreet = greetings[randomNumber(0, greetings.length-1)];
return randomGreet;

}

// Generates a random compliment
// return {string} - random compliment, followed by exclamation point
function randomCompliment(){
var compliments = [", you have incredible hair!", ", you are astonishingly smart!",
", your fashion sense is unmatched!", ", your smile lights up the room!",
", you are uncommonly talented!", ", you are ridiculously funny!"];
// Write code to complete this function

}

3 answers

To complete the randomCompliment function, you will need to select a random compliment from the array of compliments in a manner similar to how it's done in the randomGreeting function. Here's how to implement it:

// Generates a random compliment
// return {string} - random compliment, followed by exclamation point
function randomCompliment(){
    var compliments = [", you have incredible hair!", ", you are astonishingly smart!",
    ", your fashion sense is unmatched!", ", your smile lights up the room!",
    ", you are uncommonly talented!", ", you are ridiculously funny!"];
    
    var randomCompliment = compliments[randomNumber(0, compliments.length - 1)];
    return randomCompliment;
}

// Helper function to generate a random number between min and max (inclusive)
function randomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

greetAndCompliment("Benjamin");
greetAndCompliment("Raji");
greetAndCompliment("Esmerelda");

Explanation:

  1. Variable Setup: We declare an array named compliments which contains various compliment strings.
  2. Random Selection: We use the randomNumber function to get a random index within the bounds of the compliments array.
  3. Return: The selected compliment is returned from the function.

Note:

The randomNumber function is assumed to already exist in your code, as it's referenced in both greeting and compliment selection. This function is used to return a random integer within a specified range.

Not defined

To ensure that the randomNumber function is defined so it can be used in both randomGreeting and randomCompliment, you'll need to define it within your code. Here’s the complete code with the randomNumber function included:

// Generates a random greeting
// return {string} - random greeting, followed by a comma and space
function randomGreeting() {
    var greetings = ["Good day, ", "Bonne journée, ", "Buenos dias, ", "Magandang araw, ", "美好的一天, "];
    var randomGreet = greetings[randomNumber(0, greetings.length - 1)];
    return randomGreet;
}

// Generates a random compliment
// return {string} - random compliment, followed by exclamation point
function randomCompliment() {
    var compliments = [", you have incredible hair!", ", you are astonishingly smart!",
    ", your fashion sense is unmatched!", ", your smile lights up the room!",
    ", you are uncommonly talented!", ", you are ridiculously funny!"];

    var randomCompliment = compliments[randomNumber(0, compliments.length - 1)];
    return randomCompliment;
}

// Helper function to generate a random number between min and max (inclusive)
function randomNumber(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

// Main function that greets and compliments a person by name
function greetAndCompliment(name) {
    console.log(randomGreeting() + name);
    console.log(name + randomCompliment());
}

// Calling the function with different names
greetAndCompliment("Benjamin");
greetAndCompliment("Raji");
greetAndCompliment("Esmerelda");

Breakdown of Functions:

  • randomNumber(min, max): This function generates a random integer between the specified minimum and maximum values, inclusive.
  • randomGreeting(): This function returns a random greeting string.
  • randomCompliment(): This function returns a random compliment string.
  • greetAndCompliment(name): This function uses both the greeting and compliment functions to output a greeting and compliment for the given name.

Usage:

You can call the greetAndCompliment function with different names, and it will display a random greeting and compliment for each name.

Similar Questions
  1. The temporal dimension of the environment refers to the way thatA. the structure of the site is composed. B. children, staff,
    1. answers icon 2 answers
  2. The temporal dimension of the environment refers to the way thatA.The children, staff, and parents are organized in the program.
    1. answers icon 1 answer
    1. answers icon 1 answer
  3. How did South Carolinians contribute to the Harlem Renaissance?Former South Carolina governor Ben Tillman wrote a charter for
    1. answers icon 1 answer
more similar questions