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:
- Variable Setup: We declare an array named
compliments
which contains various compliment strings. - Random Selection: We use the
randomNumber
function to get a random index within the bounds of thecompliments
array. - 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.