"Modify the car-painting example so that the car is painted with your favorite color if you have one; otherwise it is painted w/ the color of your garage (if the color is known); otherwise it is painted red." Car-painting example: car.color = favoriteColor || "black";
How or what do I need to do to make my script work?
car.color = favoriteColor || "red";
if (car.color = favoriteColor ){
alert("your car is " + favoriteColor);
} else if (car.color = garageColor){
alert("your car is " + garageColor);
} else {
car.color = "red";
}
This is the correct answer to your question:
See my comments to get a better understanding of how this works.
if (favoriteColor) {
car.color = favoriteColor; // If you have a favorite color, choose the favorite color.
} else if (garageColor) {
car.color = garageColor; // If the garage color is known, choose the garage color.
} else {
car.color = 'red'; // Otherwise, choose the color 'red'.
}
alert('Your car is: ' + car.color);
Related Question (for further insight):
https://softwareengineering.stackexchange.com/q/289475
Alternative Method:
As another possibility, you can write a conditional (ternary) operation to compact all of the logic into a single statement.
alert('Your car is: ' + (car.color = favoriteColor ? favoriteColor : garageColor ? garageColor : 'red'));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments