✅ – ✅ – dmikester1 – 03-34 May 5
What am I doing wrong here?
tempRating
is always 'hot'
10 Replies
can you show the error
there is no error,
tempRating
gets set to 'hot' on every run
based on a 'temp' value of 10, it should be getting set to 'cold'instead try doing this with if else statement, instead of switch case. Above is also the reason why only default case is running.
what is the reason why only default case is running?
well if you want to run it do one thing
do
switch(true){
// your code
}
const temp = 10;
function setTempRating(val) {
console.log(
temp is ${val}
);
}
switch (true) {
case temp <= 30:
setTempRating("cold");
// console.log("first" + temp);
break;
case temp <= 50:
setTempRating("cool");
// console.log("second" + temp);
break;
case temp <= 70:
setTempRating("warm");
// console.log("third" + temp);
break;
default:
setTempRating("hot");
break;
}Stack Overflow
JavaScript: using a condition in switch case
How can I use a condition inside a switch statement for JavaScript?
In the example below, a case should match when the variable liCount is <= 5 and > 0; however, my code does not work:
switch (
check this out...
you will understand the concept, and why your code was not working
The reason why default case is always executed is because
temp
is compared to each case expression, e.g. temp === (temp <= 10)
- > temp === true
-> false. Every case condition is evaluated first (returning true, cause 10 is less than every number compared in there) and then 10 is compared to true, which gives false, so default is matchedThis question has an answer! Thank you for helping 😄
If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1103887415707451492
This question has an answer! Thank you for helping 😄
If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1103887415707451492
This thread hasn’t had any activity in 36 hours, so it’s now locked.
Threads are closed automatically after 36 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1103887415707451492