There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
About “switch”, following is written: " The break keyword stops the remaining case s from being checked and executed in a switch statement."
What you mean as “checked”? I removed brakes, and ALL case’s were executed, despite the fact, that their conditions are false.
state = 'A'
switch (state) {
case 'A':
console.log('Gold');
case 'B':
console.log('Silver');
case 'C':
console.log('Bronze');
default:
console.log('Chalk');
}
/*
Gold
Silver
Bronze
Chalk
*/
The check is the case match. Once a match is found, its branch is followed; although, in the absence of break (or return) subsequent cases are ignored, but the actions are carried out.
state = 'A'
switch (state) {
case 'A':
console.log('Gold');
break;
case 'B':
console.log('Silver');
break;
case 'C':
console.log('Bronze');
break;
default:
console.log('Chalk');
}
/*
Gold
*/
From this lesson, I learned how to create conditional statements.
Now, I’d like to know how I would combine conditional statements with user input. Like, if I were to be asking a simple question with only one correct answer, I’d like it to register and if the user types something different from that answer, it would be the wrong input; In other words, a basic if/else statement.
let correctAnswer = "yes";
let user = prompt("Are you coding like a pro?");
if (user.toLowerCase() === correctAnswer) {
console.log("Give yourself a pat on the back!");
}
else {
console.log("Keep working away at it...");
}
This is a good question since it touches upon a very important topic, that of validating user input and/or choosing the appropriate conditional brranch based upon the user’s input.
If if…else statements can be simplified using the ternary operator and if…elseif statements can be simplified using a switch steatment, if there any need for if statements at all? Are they obsolete or a sign of bad code?
You are joking, right? If it was bad code, would it have made it this far along the programming language chain?
The ‘can be simplified’ part is what throws me. If this were verifiable in all situations then it would be a one off. Unfortunately, that is not always the case, so this is not something upon which we may draw any conclusions; ergo, don’t toss the baby with the bathwater.
I wrote a piece of code (first attempt) that generates a random ‘day’ number using Math.random and combined this with conditional statements to determine which day of the week it is and subsequently log the name of that day to the console.
Since Math.random generates a number between 0 and 1, and I am not assigning the rounded number to the variable ‘day’, I assume the variable ‘day’ holds an unrounded numbervalue. What I noticed is that with comparitive operators the decimals appear to be ignored while with the switch operator your first need to round the generated ‘day’ number before it selects the correct name of the day. Here is the code I wrote that works, ie. ‘switch (Math.floor(day))’. But as soon as I change it to ‘switch (day)’, the program always logs “Not a weekday” to the console.
Can anyone explain?
Can we see any weekday numbers in the number generated by that? Even with rounding the most we can hope for is 0 or 1.
What makes this practical is having a multiplier. Since a week has seven days, then the multiplier in this case will be 7.
Math.random() * 7
Now we have the potential to generate numbers between 0, inclusive, and 7, exclusive (meaning it can’t be 7). This is where Math.floor comes into play by reducing any decimal fraction to zero, resulting in an integer in the set,
{0, 1, 2, 3, 4, 5, 6}
The only question now is what number do we wish to assign the first day of the week? A 0 or a 1? If it is 1 then we just have to add 1 to the outcome.
Thanks for your suggestion, mtf! I will certainly give this a try.
But can you also explain why ‘(day === 6 || day === 7) ? console.log(‘It’s weekend!’) : console.log(‘It’s a business day.’)’ generates the correct result even when I don’t have an integer stored as value for ‘day’ and why the switch operator turns to the default response, when I use 'switch (day) instead of ‘switch(Math.floor(day))’?
OR short-circuits on true, but when both are false, the outcome is false. If day is not defined then it will result in false or will raise an error is day is not declared.
It looks like your program treats Monday as day 1 and Sunday as day 7. A little bit non-standard since many of us treat Sunday as the first day of the new week. No matter, so long as our program is sorted out.
I noticed your code uses 10 as the multiplier. Did you change that to 7?
A ternary expression can be written as the argument for console.log()…
console.log(day > 6 ? "It's the weekend!" : "It's a business day.')
Neither a switch nor an if…else if is needed for this since we have a sequence that repeats, weekly.
Thanks again, mtf! Changed the multiplier to 7. Works great.
And thanks for the suggestion of using an array. Didn’t cover that yet in the training.
About Monday being day 1 and Sunday day 7 being non-standard, I guess that is debateable. Google Calendar has Saturday and Sunday at the end of the week (ie. day 6 and 7):
What is standard, any more? Likely four-fifths of the world’s internet users are outside of North America so Google is likely favoring what is standard to them. Misnomer on my part.
Just had a quick question, I tried doing my own magic 8 ball project since I don’t have access to pro.
The syntax is correct it’s just that the code always logs the default into the console and I don’t know why. Here is the code below
hey guys just a quick question. my friends have liquor business and they are having a hard time keeping the records of payments. do you think i can use the switch condition to help them keep track of the payments coming in?
What I understood of the NOT operator is that it reverses the truthiness of a value. So if we had a truthy value (E.g. 1) and we logged !1, it would log false to the console. However, what I don’t understand is when writing if (!condition). Without the NOT operator, the condition would translate to, “is the condition truthy?”. Does that mean that with the NOT operator, the condition translates to “is the condition falsy”?