Where is the error in the code?

0 like 0 dislike
3 views
Good day to all.

Comrades, what is the error in my understanding of the logic Js. Please explain and if it does not consider the extra - correct.

var trouble = prompt('What is wrong?') var cabel = 1 var router = 2 var computer = 3 if (trouble == 1) { console.log('check your cabel') } else if (trouble == 2){ console.log('check your router') } else if (trouble == 3) { console.log('reload your PC') } else { console.log('uncorrect trouble') }
by | 3 views

4 Answers

0 like 0 dislike
var trouble = +prompt('What is wrong?'); var CABEL = 1; var ROUTER = 2; var COMPUTER = 3; switch (trouble) { case CABEL: console.log('check your cabel'); break; case ROUTER: console.log('check your router'); break; COMPUTER case: console.log('reload your PC'); break; default: console.log('uncorrect trouble'); }
by
0 like 0 dislike
var trouble = prompt('What is wrong?'); switch (trouble) { case 1: console.log('check your cabel'); break; case 2: console.log('check your router'); break; case 3: console.log('reload your PC'); break; default: console.log('uncorrect trouble'); break; }
by
0 like 0 dislike
1) prompt returns a string, the comparison is with numbers - incorrect
2) blocks with if ()... you are always comparing with constants, although earlier they announced their "meaning"

Accordingly, it would be better to do so:

1) var trouble = parseInt(prompt('What is wrong?'))
2) if (trouble == cabel) {...
...
else if (trouble == router) { ...
...
else if (trouble == computer) { ....
by
0 like 0 dislike
I have Your code working
here
And do not neglect the ";" at the end of the row.
by

Related questions

0 like 0 dislike
1 answer
asked Sep 7, 2019 by vladislav9619996
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Aug 3, 2019 by vladislav9619996
0 like 0 dislike
1 answer
asked Jul 8, 2019 by vladislav9619996
0 like 0 dislike
1 answer
asked Jul 10, 2019 by vladislav9619996
110,608 questions
257,186 answers
0 comments
33,707 users