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

Multi tool use
Multi tool use

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













-2















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.










share|improve this question















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
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 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 your count == '2' is not same as case 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















-2















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.










share|improve this question















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
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 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 your count == '2' is not same as case 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













-2












-2








-2








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
If this question can be reworded to fit the rules in the help center, please edit the question.







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
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 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 your count == '2' is not same as case 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





    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 your count == '2' is not same as case 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












2 Answers
2






active

oldest

votes


















7














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.







share|improve this answer








New contributor




poniraq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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



















-2














Looks like you have weird quote characters in your switch:



enter image description here



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.






share|improve this answer


















  • 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

















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









7














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.







share|improve this answer








New contributor




poniraq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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
















7














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.







share|improve this answer








New contributor




poniraq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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














7












7








7







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.







share|improve this answer








New contributor




poniraq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










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.








share|improve this answer








New contributor




poniraq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer






New contributor




poniraq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered Mar 21 at 16:08









poniraqponiraq

861




861




New contributor




poniraq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





poniraq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






poniraq is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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

















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














-2














Looks like you have weird quote characters in your switch:



enter image description here



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.






share|improve this answer


















  • 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















-2














Looks like you have weird quote characters in your switch:



enter image description here



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.






share|improve this answer


















  • 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













-2












-2








-2







Looks like you have weird quote characters in your switch:



enter image description here



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.






share|improve this answer













Looks like you have weird quote characters in your switch:



enter image description here



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.







share|improve this answer












share|improve this answer



share|improve this answer










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












  • 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



IYalUTClAxtmprleZisK 0 0tVRnDyPyeSeoa,7KGwld26MtK8qc U Skg
LiYo5CG,b27LD53xS R,uHureFywZFrS,B7 VFWI,FYXu5 eHhb0,Kc,Zd qFSAZGoGOBiaLiYZ,HIqePkVEoozf,htzZklbSC,d

Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현