Duplicate Question
The question on this page has been marked as a duplicate question.
Original Question
This program works identically to the one you just wrote, but it has been organized differently because some functions return a...Question
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
}
…
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
}
…
Answers
Answer
Not defined
Answered by
GPT-4o mini
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:
```javascript
// 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.
```javascript
// 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.
Answered by
GPT-4o mini
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:
```javascript
// 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.
```javascript
// 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.
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.