216k views
3 votes
The museum ticket price should be :

$0 on Fridays with couponcode "FREEFRIDAY"
$10 on the weekends for everybody
On weekdays $5 for 18 years old and under and $10 otherwise.
A student wrote this conditional to set the price . For which case will the price NOT come out as indicated?

var price=10;

// Check the value of variables to decide the price to set

if (age <= 18 && day != "Saturday" && day != "Sunday") {
price = price / 2;
} else if (day == "Friday" && discountCode == "FREEFRIDAY"){
price = 0;
}

a. a minor on Friday with the discount code
b. an adult on Friday with the discount code
c. an adult on the weekend
d. a minor on the weekend
e. an adult on a weekday

User Laziale
by
7.5k points

1 Answer

2 votes

Answer:

a. a minor on Friday with the discount code

Step-by-step explanation:

Let's try and decipher the given code segment

Let's first look at the if part

if (age <= 18 && day != "Saturday" && day != "Sunday") {

price = price / 2;

==> if a minor and a weekend then price is half = $5

Now look at the else part

else if (day == "Friday" && discountCode == "FREEFRIDAY"){

price = 0;

}

This means if the visitor is NOT a minor and it is NOT a weekend if it is a friday, with a coupon then admission is free

Let's look at the answer choices and see what the logic flow is:

a. minor on Friday with the discount code
if (age <= 18 && day != "Saturday" && day != "Sunday")
All three parts of this condition are true so the statement price = price/2 is executed and price = 5. However, everyone with this coupon should get in free. So this part is incorrectly coded

b. an adult on Friday with the discount code
if part is False, else if part is true and price = 0 which is consistent

c. an adult on the weekend
if part is false, else if part is also false so price printed out is 10 which is consistent

d. a minor on the weekend
the if part is false and so is the else if part , so price is unchanged at 10. Consistent

e. an adult on a weekday
Both the if and else if parts are false, so price is unchanged at 10. COnsistent

User Dtell
by
8.0k points