dmikester1
dmikester115mo ago

✅ – ✅ – dmikester1 – 03-34 May 5

What am I doing wrong here? tempRating is always 'hot'
const temp = 10; //r.data.weatherData.temp;
console.log({ temp });
switch (temp) {
case temp <= 30:
setTempRating('cold');
break;
case temp <= 50:
setTempRating('cool');
break;
case temp <= 70:
setTempRating('warm');
break;
default:
setTempRating('hot');
break;
}
const temp = 10; //r.data.weatherData.temp;
console.log({ temp });
switch (temp) {
case temp <= 30:
setTempRating('cold');
break;
case temp <= 50:
setTempRating('cool');
break;
case temp <= 70:
setTempRating('warm');
break;
default:
setTempRating('hot');
break;
}
10 Replies
NishantJoshi
NishantJoshi15mo ago
can you show the error
dmikester1
dmikester115mo ago
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'
NishantJoshi
NishantJoshi15mo ago
instead try doing this with if else statement, instead of switch case. Above is also the reason why only default case is running.
dmikester1
dmikester115mo ago
what is the reason why only default case is running?
NishantJoshi
NishantJoshi15mo ago
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; }
NishantJoshi
NishantJoshi15mo ago
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 (
NishantJoshi
NishantJoshi15mo ago
check this out... you will understand the concept, and why your code was not working
ScriptyChris
ScriptyChris15mo ago
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 matched
reactibot
reactibot15mo ago
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
reactibot
reactibot15mo ago
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