+ 1

javascript switch

let age = prompt("Enter Your age: ") age = Number.parseInt(age) switch (age) { case age < 0: console.log('Invalid age...') break; case age > 0 && age < 18: console.log('You can\'t vote') break; default: console.log('You can vote') } // not able to find correct ans always showing "You can vote"

16th Nov 2022, 8:02 AM
Amol Bhandekar
Amol Bhandekar - avatar
2 Réponses
+ 4
Now run the code:- let age = prompt("Enter Your age: ") age = parseInt(age) switch (true) { case age < 0: console.log('Invalid age...') break; case age > 0 && age < 18: console.log('You can\'t vote') break; default: console.log('You can vote') }
16th Nov 2022, 8:53 AM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 1
Thanks @sakshi Its working :)
16th Nov 2022, 9:00 AM
Amol Bhandekar
Amol Bhandekar - avatar