why wont "||" (or) work in my javascript function?

Viewed 35

I am trying to write the code so that if if given 2 numbers that represent my birthday (month = 11, day = 3) it would log "how did you know". anything else would return "Just Another Day". Also need the order of month and day to not matter.

I have written it as:

function birthday (month,day){
  let result;
  if( month == "11" && day == "3") || (day == "3" && month = "11"){
      result = "How did you know?";
     }
      else {
       result = "Just Another Day";
      }
  return result;
}

console.log(birthday(3, 11));

but it returns and tell me the || (or) is an unexpected token...How else could I write this?

3 Answers

You have two conditions on the two sides of the || operator that say the exact same thing. On the second condition, you need to check if the day is 11 and the month 3. Additionally, you have = operator on the last comparison instead of a ==:

if ((month == "11" && day == "3") || (month == "3" && day == "11")) {

You should add "(" after if and add ")" after (day == "3" && month == "11").Also change = operator on the last comparison to == and change last comparison from (day == "3" && month == "11") to (month == "3" && day == "11"). Change your code to this code:

function birthday (month,day){
      let result;
      if( (month == "11" && day == "3") || (month == "3" && day == "11")){
          result = "How did you know?";
         }
          else {
           result = "Just Another Day";
          }
      return result;
    }
    
    console.log(birthday(3, 11));

I modify your code, the last comparison symbol (==) was wrong, it was an asignment symbol(=) and you are comparing day and month twice, now your code works fine!

function birthday (month, day){
  let result = "";

  if(( month == "11" && day == "3") || (month == "3" && day == "11")){
      result = "How did you know?";
     }
      else {
       result = "Just Another Day";
      }
  return result;
}

console.log(birthday(3, 11));

Related