+ 1

Even Odd Program

Is it possible to write a program in javascript which can take the number as input and output whether it's even or odd But wait you can't use any type of conditional statement (no if else, no switch case). Yes or No... If yes write it down below..... No chatGpt or any AI assistance.

17th Jan 2025, 8:34 AM
Mustafa Raza
6 odpowiedzi
+ 3
0 and 1 can also be used as index of a list or array. so we are not using switch case or if-else... n & 1 works like n%2 console.log(['even', 'odd'][prompt() & 1])
17th Jan 2025, 11:07 AM
Bob_Li
Bob_Li - avatar
+ 2
I will insert it. https://sololearn.com/compiler-playground/WdnR27nrMZh1/?ref=app you can't do that without selection. but you can do that without the % operator.
17th Jan 2025, 12:23 PM
Arash Amorzesh
Arash Amorzesh - avatar
+ 1
Arash Amorzesh the OP said not to use any type of conditional statement, you used a tenerary operator which is probably against the rules.
17th Jan 2025, 1:51 PM
Zvi
Zvi - avatar
+ 1
Mustafa Raza Modified Bob_Li's code a little bit. console.log(['even','odd'][prompt()%2]);
17th Jan 2025, 2:01 PM
Gulshan Mahawar
Gulshan Mahawar - avatar
+ 1
Zvi Thanks.
17th Jan 2025, 2:18 PM
Arash Amorzesh
Arash Amorzesh - avatar
0
Gulshan Mahawar actually I think Bob_li’s solution is way cooler. I had to ask chatGTP to explain it and I still only get the basic idea!
17th Jan 2025, 5:53 PM
Zvi
Zvi - avatar