-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
  • 18<x<12 this doesn't mean what you think it means. Commented Nov 9, 2020 at 18:41
  • 6
    18<x<121 is (18 < x) < 121 which is either true < 121 or false < 121 which are 1 < 121 or 0 < 121 respectively, which is always true. Commented Nov 9, 2020 at 18:41

2 Answers 2

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")} 
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.