+ 1
JS
Hi Please look through my code. If there is a mistake let me know. Thanks in advance! let color = 'green'; /* red => 1 green => 2 black => 3 other cases => unknown */ //your code goes here if (color == 'red') { console.log('1'); } else if (color == 'green') { console.log('2'); } else if (color == 'black') { console.log('3'); } else { console.log('Unknown!'); }
2 Respuestas
+ 4
Add task requirement in post Description for better view of the problem.
+ 3
Your code has no problem, however in JS, it is recommended to use === than == for equivalent comparison.
There would be bug that might be hard to find and debug in a more complex codes if you use == for equivalent comparison. E.g.
let i = 1;
if (i == '1') {
console.log('Thus will run');
}
if (i == '01') {
console.log('This also run!');
}
if (i == '0001') {
console.log('This run too!!');
}
if (i == '0x001') {
console.log('This run as well!!!');
}
https://code.sololearn.com/cBi0oxhWJWh1/?ref=app
JavaScript is different from other languages like C or Java, it does not require a data type to be declared, this makes it very risky to use uncheck equivalent comparison.
Other languages use == safely because of they have type check beforehand.
(I have been coding JavaScript codes for many years, I remember I never use ==, not even once for my projects)