
Annalise W.
asked 03/31/20Write a code segment that will store a dinner selection in option1 based on the values of rsvp and selection. The intended behavior of the code segment is described below.
Write a code segment that will store a dinner selection in option1 based on the values of
rsvp and selection. The intended behavior of the code segment is described below.
If rsvp is true, the code segment should store in option1 a string indicating the person's
attendance and food choice. For example, if rsvp is true and selection is 1, the following
string should be stored in optioni.
"Thanks for attending. You will be served beef."
If rsvp is false, the following string should be stored in option1, regardless of the value of
selection.
"Sorry you can't make it."
Write the code segment below. Your code segment should meet all specifications and conform
to the examples.
2 Answers By Expert Tutors
Dorothy P. answered 04/01/20
Software Engineer, Game Developer, Math and Humanities Tutor
You didn't specify a language, but there's some general concepts involved here.
Key concepts in this question include:
* Boolean (true/false) logic
* If/else structures
* Variable creation and assignment
* String concatenation
Here's the basic outline of steps we need to perform:
1. Create an empty string variable called option1 (assuming it hasn't been created at the start
of the question).
2. Check the value of rsvp
3. Take the proper action based on rsvp's value
3a. If rsvp is "true":
- Store the value "Thanks for attending. You will be served " in the option1
variable.
- Then, append the value of the selection variable to the end of option1, plus
a period.
3b. Else if rsvp is "false":
- Store the value "Sorry you can't make it." in the option variable.
Writing the steps out this way can help you to make better sense of the problem statement.
Now let's add some code to these steps. I'll assume that this is a language like JavaScript where
you don't have to say what data type each variable is.
So let's create the empty string variable, option1, as in Step 1:
var option1 = ""; //Now we have a string named option1, and it's set to empty at first.
Now we will take 1 of two options based on the value of rsvp, so we will need an if/else block structure. Note, I'm assuming that the value for rsvp and selection have been provided somewhere. From
the sounds of it, rsvp is a boolean (true or false) value.
We'll start this block structure off with Step 2:
if(rsvp) //Here we're checking the value of rsvp. If rsvp is true, we will do what's in the next set of braces
{
option1.concat(Thanks for attending. You will be served ", selection, ".");
//This is step 3a. Calling concat on a string variable will add the strings in the parentheses
//to the string variable, at the end. We can add multiple strings at once by separating them by
//commas. Notice that we can append both string literals (the text between "" marks) and variables
//(like the selection variable).
}
else //rsvp can only be one of two values: true or false. So, if our "if" statement found that rsvp was
//not true, the only other option is to come to this else block
{
option1.concat("Sorry you can't make it.");
//This completes Step 3b.
}
You can learn more about the concat function at https://www.w3schools.com/jsref/jsref_concat_string.asp
This is just how you would do it in JavaScript. Let me know if you need a different language. The concept should be similar.
Let me know if this helps or if you need more explanation!
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Milad G.
Hi, Please clarify what programming language this assignment is for, and if any skeleton code or extra information is given, for example, it seems like there should be list of information given about numerical option values that correspond to dinner choices, please provide those as well and if they are presented in some sort of predefined list by the program or the problem set.04/01/20