Why does my if / else if statement work but my switch case doesn't? [closed]Javascript: How to use object literals instead of if and switch statements for expression-based conditions?Why don't self-closing script tags work?How does JavaScript .prototype work?Why does Google prepend while(1); to their JSON responses?How does the “this” keyword work?Why does ++[[]][+[]]+[+[]] return the string “10”?How does data binding work in AngularJS?How does Access-Control-Allow-Origin header work?Why does parseInt(1/0, 19) return 18?Switch statement multiple cases in JavaScriptWhy does my javascript else statement stop working
Trouble understanding the speech of overseas colleagues
System.debug(JSON.Serialize(o)) Not longer shows full string
Sequence of Tenses: Translating the subjunctive
How to draw lines on a tikz-cd diagram
Increase performance creating Mandelbrot set in python
How to run a prison with the smallest amount of guards?
Pole-zeros of a real-valued causal FIR system
Type int? vs type int
Tiptoe or tiphoof? Adjusting words to better fit fantasy races
Do all network devices need to make routing decisions, regardless of communication across networks or within a network?
Do the temporary hit points from Reckless Abandon stack if I make multiple attacks on my turn?
Anatomically Correct Strange Women In Ponds Distributing Swords
India just shot down a satellite from the ground. At what altitude range is the resulting debris field?
For a non-Jew, is there a punishment for not observing the 7 Noahide Laws?
Are student evaluations of teaching assistants read by others in the faculty?
Is HostGator storing my password in plaintext?
Applicability of Single Responsibility Principle
Short story about space worker geeks who zone out by 'listening' to radiation from stars
What is the intuitive meaning of having a linear relationship between the logs of two variables?
Hostile work environment after whistle-blowing on coworker and our boss. What do I do?
Is there a problem with hiding "forgot password" until it's needed?
How long to clear the 'suck zone' of a turbofan after start is initiated?
Is expanding the research of a group into machine learning as a PhD student risky?
How do scammers retract money, while you can’t?
Why does my if / else if statement work but my switch case doesn't? [closed]
Javascript: How to use object literals instead of if and switch statements for expression-based conditions?Why don't self-closing script tags work?How does JavaScript .prototype work?Why does Google prepend while(1); to their JSON responses?How does the “this” keyword work?Why does ++[[]][+[]]+[+[]] return the string “10”?How does data binding work in AngularJS?How does Access-Control-Allow-Origin header work?Why does parseInt(1/0, 19) return 18?Switch statement multiple cases in JavaScriptWhy does my javascript else statement stop working
So I have run into this problem which has been driving me nuts for the past couple of hours.
I have a conditional statement:
if (count == '0')
var variable = '0%'
document.querySelector('.content').innerHTML = variable
else if (count == '1')
var variable = '5%'
document.querySelector('.content').innerHTML = variable
else if (count == '2')
var variable = '10%'
document.querySelector('.content').innerHTML = variable
and it's working fine. The only problem is, I have 20 values so instead of writing the same if/else if
for 20 values I decided to make a switch
case:
switch (count)
case '0':
var variable = '0 %'
document.querySelector('.content').innerHTML = variable
break;
case '1':
var variable = '5 %'
document.querySelector('.content').innerHTML = variable
break;
For some reason its not working, I really don't understand what's wrong.
TLDR: My if/else if
is working fine but my switch
case isn't and I can't figure out why not.
javascript
closed as off-topic by Barmar, Get Off My Lawn, Code Maniac, ponury-kostek, Jared Smith Mar 21 at 16:10
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Barmar, Get Off My Lawn, Code Maniac, ponury-kostek, Jared Smith
|
show 13 more comments
So I have run into this problem which has been driving me nuts for the past couple of hours.
I have a conditional statement:
if (count == '0')
var variable = '0%'
document.querySelector('.content').innerHTML = variable
else if (count == '1')
var variable = '5%'
document.querySelector('.content').innerHTML = variable
else if (count == '2')
var variable = '10%'
document.querySelector('.content').innerHTML = variable
and it's working fine. The only problem is, I have 20 values so instead of writing the same if/else if
for 20 values I decided to make a switch
case:
switch (count)
case '0':
var variable = '0 %'
document.querySelector('.content').innerHTML = variable
break;
case '1':
var variable = '5 %'
document.querySelector('.content').innerHTML = variable
break;
For some reason its not working, I really don't understand what's wrong.
TLDR: My if/else if
is working fine but my switch
case isn't and I can't figure out why not.
javascript
closed as off-topic by Barmar, Get Off My Lawn, Code Maniac, ponury-kostek, Jared Smith Mar 21 at 16:10
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Barmar, Get Off My Lawn, Code Maniac, ponury-kostek, Jared Smith
7
You are using the wrong type of quotes
– Get Off My Lawn
Mar 21 at 16:02
6
‘
that quote character is strange. I don't think JS allows it.
– Sergiu Paraschiv
Mar 21 at 16:02
5
Why not just use...innerHTML = `$count * 5%`
?
– p.s.w.g
Mar 21 at 16:02
9
Switch uses===
so yourcount == '2'
is not same ascase 2
– Code Maniac
Mar 21 at 16:04
3
Are you passing a string or an int into the switch statement. 1 !== '1'
– Ardesco
Mar 21 at 16:05
|
show 13 more comments
So I have run into this problem which has been driving me nuts for the past couple of hours.
I have a conditional statement:
if (count == '0')
var variable = '0%'
document.querySelector('.content').innerHTML = variable
else if (count == '1')
var variable = '5%'
document.querySelector('.content').innerHTML = variable
else if (count == '2')
var variable = '10%'
document.querySelector('.content').innerHTML = variable
and it's working fine. The only problem is, I have 20 values so instead of writing the same if/else if
for 20 values I decided to make a switch
case:
switch (count)
case '0':
var variable = '0 %'
document.querySelector('.content').innerHTML = variable
break;
case '1':
var variable = '5 %'
document.querySelector('.content').innerHTML = variable
break;
For some reason its not working, I really don't understand what's wrong.
TLDR: My if/else if
is working fine but my switch
case isn't and I can't figure out why not.
javascript
So I have run into this problem which has been driving me nuts for the past couple of hours.
I have a conditional statement:
if (count == '0')
var variable = '0%'
document.querySelector('.content').innerHTML = variable
else if (count == '1')
var variable = '5%'
document.querySelector('.content').innerHTML = variable
else if (count == '2')
var variable = '10%'
document.querySelector('.content').innerHTML = variable
and it's working fine. The only problem is, I have 20 values so instead of writing the same if/else if
for 20 values I decided to make a switch
case:
switch (count)
case '0':
var variable = '0 %'
document.querySelector('.content').innerHTML = variable
break;
case '1':
var variable = '5 %'
document.querySelector('.content').innerHTML = variable
break;
For some reason its not working, I really don't understand what's wrong.
TLDR: My if/else if
is working fine but my switch
case isn't and I can't figure out why not.
javascript
javascript
edited Mar 21 at 16:08
OliverRadini
3,113728
3,113728
asked Mar 21 at 16:00
jortonjorton
195
195
closed as off-topic by Barmar, Get Off My Lawn, Code Maniac, ponury-kostek, Jared Smith Mar 21 at 16:10
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Barmar, Get Off My Lawn, Code Maniac, ponury-kostek, Jared Smith
closed as off-topic by Barmar, Get Off My Lawn, Code Maniac, ponury-kostek, Jared Smith Mar 21 at 16:10
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – Barmar, Get Off My Lawn, Code Maniac, ponury-kostek, Jared Smith
7
You are using the wrong type of quotes
– Get Off My Lawn
Mar 21 at 16:02
6
‘
that quote character is strange. I don't think JS allows it.
– Sergiu Paraschiv
Mar 21 at 16:02
5
Why not just use...innerHTML = `$count * 5%`
?
– p.s.w.g
Mar 21 at 16:02
9
Switch uses===
so yourcount == '2'
is not same ascase 2
– Code Maniac
Mar 21 at 16:04
3
Are you passing a string or an int into the switch statement. 1 !== '1'
– Ardesco
Mar 21 at 16:05
|
show 13 more comments
7
You are using the wrong type of quotes
– Get Off My Lawn
Mar 21 at 16:02
6
‘
that quote character is strange. I don't think JS allows it.
– Sergiu Paraschiv
Mar 21 at 16:02
5
Why not just use...innerHTML = `$count * 5%`
?
– p.s.w.g
Mar 21 at 16:02
9
Switch uses===
so yourcount == '2'
is not same ascase 2
– Code Maniac
Mar 21 at 16:04
3
Are you passing a string or an int into the switch statement. 1 !== '1'
– Ardesco
Mar 21 at 16:05
7
7
You are using the wrong type of quotes
– Get Off My Lawn
Mar 21 at 16:02
You are using the wrong type of quotes
– Get Off My Lawn
Mar 21 at 16:02
6
6
‘
that quote character is strange. I don't think JS allows it.– Sergiu Paraschiv
Mar 21 at 16:02
‘
that quote character is strange. I don't think JS allows it.– Sergiu Paraschiv
Mar 21 at 16:02
5
5
Why not just use
...innerHTML = `$count * 5%`
?– p.s.w.g
Mar 21 at 16:02
Why not just use
...innerHTML = `$count * 5%`
?– p.s.w.g
Mar 21 at 16:02
9
9
Switch uses
===
so your count == '2'
is not same as case 2
– Code Maniac
Mar 21 at 16:04
Switch uses
===
so your count == '2'
is not same as case 2
– Code Maniac
Mar 21 at 16:04
3
3
Are you passing a string or an int into the switch statement. 1 !== '1'
– Ardesco
Mar 21 at 16:05
Are you passing a string or an int into the switch statement. 1 !== '1'
– Ardesco
Mar 21 at 16:05
|
show 13 more comments
2 Answers
2
active
oldest
votes
It might be type-conversion issue.
With "if" approach you're using type-converting comparison operator (==), so 0 == '0'
yields true
.
Switch, on the other hand, uses strict comparison operator.
https://www.w3schools.com/js/js_switch.asp
Strict Comparison
Switch cases use strict comparison (===).
The values must be of the same type to match.
A strict comparison can only be true if the operands are of the same type.
New contributor
I see thatlet count = 0
doesn't work in a switch butlet count ='0'
does work. Never thought of that good find!
– Get Off My Lawn
Mar 21 at 16:14
add a comment |
Looks like you have weird quote characters in your switch:
The switch version looks like it is using "smart quotes" which are slanted depending on whether they are opening or closing. Many rich text editors convert regular quotes to smart quotes automatically, so if you wrote the code in a rich text originally, or copied and pasted it via Slack or something, that might explain where those characters came from.
Easy fix, though, just change them to regular quote characters as you have in your if statements.
1
Whenever I've seen this issue, it's usually because the code has been copy-pasted into a word processor (e.g. Word). IDE's will usually complain pretty loudly about this, or even automagically convert them back to 'simple' single-quotes. In other words, this is most likely due to the way OP was preparing the question for StackOverflow, rather than being an actual problem in the code.
– p.s.w.g
Mar 21 at 16:12
Oh, yeah, OK, that just looked like the most obvious immediate problem, the type coercion thing makes sense after that.
– Benjamin Robinson
Mar 22 at 17:18
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It might be type-conversion issue.
With "if" approach you're using type-converting comparison operator (==), so 0 == '0'
yields true
.
Switch, on the other hand, uses strict comparison operator.
https://www.w3schools.com/js/js_switch.asp
Strict Comparison
Switch cases use strict comparison (===).
The values must be of the same type to match.
A strict comparison can only be true if the operands are of the same type.
New contributor
I see thatlet count = 0
doesn't work in a switch butlet count ='0'
does work. Never thought of that good find!
– Get Off My Lawn
Mar 21 at 16:14
add a comment |
It might be type-conversion issue.
With "if" approach you're using type-converting comparison operator (==), so 0 == '0'
yields true
.
Switch, on the other hand, uses strict comparison operator.
https://www.w3schools.com/js/js_switch.asp
Strict Comparison
Switch cases use strict comparison (===).
The values must be of the same type to match.
A strict comparison can only be true if the operands are of the same type.
New contributor
I see thatlet count = 0
doesn't work in a switch butlet count ='0'
does work. Never thought of that good find!
– Get Off My Lawn
Mar 21 at 16:14
add a comment |
It might be type-conversion issue.
With "if" approach you're using type-converting comparison operator (==), so 0 == '0'
yields true
.
Switch, on the other hand, uses strict comparison operator.
https://www.w3schools.com/js/js_switch.asp
Strict Comparison
Switch cases use strict comparison (===).
The values must be of the same type to match.
A strict comparison can only be true if the operands are of the same type.
New contributor
It might be type-conversion issue.
With "if" approach you're using type-converting comparison operator (==), so 0 == '0'
yields true
.
Switch, on the other hand, uses strict comparison operator.
https://www.w3schools.com/js/js_switch.asp
Strict Comparison
Switch cases use strict comparison (===).
The values must be of the same type to match.
A strict comparison can only be true if the operands are of the same type.
New contributor
New contributor
answered Mar 21 at 16:08
poniraqponiraq
861
861
New contributor
New contributor
I see thatlet count = 0
doesn't work in a switch butlet count ='0'
does work. Never thought of that good find!
– Get Off My Lawn
Mar 21 at 16:14
add a comment |
I see thatlet count = 0
doesn't work in a switch butlet count ='0'
does work. Never thought of that good find!
– Get Off My Lawn
Mar 21 at 16:14
I see that
let count = 0
doesn't work in a switch but let count ='0'
does work. Never thought of that good find!– Get Off My Lawn
Mar 21 at 16:14
I see that
let count = 0
doesn't work in a switch but let count ='0'
does work. Never thought of that good find!– Get Off My Lawn
Mar 21 at 16:14
add a comment |
Looks like you have weird quote characters in your switch:
The switch version looks like it is using "smart quotes" which are slanted depending on whether they are opening or closing. Many rich text editors convert regular quotes to smart quotes automatically, so if you wrote the code in a rich text originally, or copied and pasted it via Slack or something, that might explain where those characters came from.
Easy fix, though, just change them to regular quote characters as you have in your if statements.
1
Whenever I've seen this issue, it's usually because the code has been copy-pasted into a word processor (e.g. Word). IDE's will usually complain pretty loudly about this, or even automagically convert them back to 'simple' single-quotes. In other words, this is most likely due to the way OP was preparing the question for StackOverflow, rather than being an actual problem in the code.
– p.s.w.g
Mar 21 at 16:12
Oh, yeah, OK, that just looked like the most obvious immediate problem, the type coercion thing makes sense after that.
– Benjamin Robinson
Mar 22 at 17:18
add a comment |
Looks like you have weird quote characters in your switch:
The switch version looks like it is using "smart quotes" which are slanted depending on whether they are opening or closing. Many rich text editors convert regular quotes to smart quotes automatically, so if you wrote the code in a rich text originally, or copied and pasted it via Slack or something, that might explain where those characters came from.
Easy fix, though, just change them to regular quote characters as you have in your if statements.
1
Whenever I've seen this issue, it's usually because the code has been copy-pasted into a word processor (e.g. Word). IDE's will usually complain pretty loudly about this, or even automagically convert them back to 'simple' single-quotes. In other words, this is most likely due to the way OP was preparing the question for StackOverflow, rather than being an actual problem in the code.
– p.s.w.g
Mar 21 at 16:12
Oh, yeah, OK, that just looked like the most obvious immediate problem, the type coercion thing makes sense after that.
– Benjamin Robinson
Mar 22 at 17:18
add a comment |
Looks like you have weird quote characters in your switch:
The switch version looks like it is using "smart quotes" which are slanted depending on whether they are opening or closing. Many rich text editors convert regular quotes to smart quotes automatically, so if you wrote the code in a rich text originally, or copied and pasted it via Slack or something, that might explain where those characters came from.
Easy fix, though, just change them to regular quote characters as you have in your if statements.
Looks like you have weird quote characters in your switch:
The switch version looks like it is using "smart quotes" which are slanted depending on whether they are opening or closing. Many rich text editors convert regular quotes to smart quotes automatically, so if you wrote the code in a rich text originally, or copied and pasted it via Slack or something, that might explain where those characters came from.
Easy fix, though, just change them to regular quote characters as you have in your if statements.
answered Mar 21 at 16:09
Benjamin RobinsonBenjamin Robinson
36027
36027
1
Whenever I've seen this issue, it's usually because the code has been copy-pasted into a word processor (e.g. Word). IDE's will usually complain pretty loudly about this, or even automagically convert them back to 'simple' single-quotes. In other words, this is most likely due to the way OP was preparing the question for StackOverflow, rather than being an actual problem in the code.
– p.s.w.g
Mar 21 at 16:12
Oh, yeah, OK, that just looked like the most obvious immediate problem, the type coercion thing makes sense after that.
– Benjamin Robinson
Mar 22 at 17:18
add a comment |
1
Whenever I've seen this issue, it's usually because the code has been copy-pasted into a word processor (e.g. Word). IDE's will usually complain pretty loudly about this, or even automagically convert them back to 'simple' single-quotes. In other words, this is most likely due to the way OP was preparing the question for StackOverflow, rather than being an actual problem in the code.
– p.s.w.g
Mar 21 at 16:12
Oh, yeah, OK, that just looked like the most obvious immediate problem, the type coercion thing makes sense after that.
– Benjamin Robinson
Mar 22 at 17:18
1
1
Whenever I've seen this issue, it's usually because the code has been copy-pasted into a word processor (e.g. Word). IDE's will usually complain pretty loudly about this, or even automagically convert them back to 'simple' single-quotes. In other words, this is most likely due to the way OP was preparing the question for StackOverflow, rather than being an actual problem in the code.
– p.s.w.g
Mar 21 at 16:12
Whenever I've seen this issue, it's usually because the code has been copy-pasted into a word processor (e.g. Word). IDE's will usually complain pretty loudly about this, or even automagically convert them back to 'simple' single-quotes. In other words, this is most likely due to the way OP was preparing the question for StackOverflow, rather than being an actual problem in the code.
– p.s.w.g
Mar 21 at 16:12
Oh, yeah, OK, that just looked like the most obvious immediate problem, the type coercion thing makes sense after that.
– Benjamin Robinson
Mar 22 at 17:18
Oh, yeah, OK, that just looked like the most obvious immediate problem, the type coercion thing makes sense after that.
– Benjamin Robinson
Mar 22 at 17:18
add a comment |
7
You are using the wrong type of quotes
– Get Off My Lawn
Mar 21 at 16:02
6
‘
that quote character is strange. I don't think JS allows it.– Sergiu Paraschiv
Mar 21 at 16:02
5
Why not just use
...innerHTML = `$count * 5%`
?– p.s.w.g
Mar 21 at 16:02
9
Switch uses
===
so yourcount == '2'
is not same ascase 2
– Code Maniac
Mar 21 at 16:04
3
Are you passing a string or an int into the switch statement. 1 !== '1'
– Ardesco
Mar 21 at 16:05