-1

Guys i was trying to solve Caesar`s cipher and I wrote a perfect code there is one problem making it not work and i will demonstrate it with a simpler code

var str ="abcz"
let x = str.charCodeAt(3); 
console.log(x) //=122
if(18<x<121){console.log("Too young")}   // it always goes to this function even if i get the code of a,b,c
else if(x>121) {console.log("zaza")} //it never reaches this point

if someone can help explain why this code works like this to me I would be grateful Thanks in advance

2 Answers2

0

Do it like this instead

var str ="abcz"
let x = str.charCodeAt(3); 
console.log(x) //=122

if(18 < x && x < 121){console.log("Too young")}   

else if(x>121) {console.log("zaza")} 
BlowFish
  • 326
  • 4
  • 10
0

I think the JS interpreter first evaluates if (18 < 122) to true and then if (true < 121) to true. My assumption is that true is converted to 1 to make a numeric comparison with 121. Your condition should look something like this if (x > 18 && x < 121)

Ogod
  • 878
  • 1
  • 7
  • 15