알고리즘 46 - Opposites Attract

jabae·2021년 10월 28일
0

알고리즘

목록 보기
46/97

Q.

Timmy & Sarah think they are in love, but around where they live, they will only know once they pick a flower each. If one of the flowers has an even number of petals and the other has an odd number of petals it means they are in love.

Write a function that will take the number of petals of each flower and return true if they are in love and false if they aren't.

A)

function lovefunc(flower1, flower2){
  if (flower1 === 0 && flower2 === 0)
    return false
  return (flower1 % 2 === 0 && flower2 % 2 === 1) || 
        (flower1 % 2 === 1 && flower2 % 2 === 0)
}

other

와 미쳤다... 어떻게 이렇게 풀 생각을 하는 걸까... 너무 손쉽다.

function lovefunc(flower1, flower2){
  return flower1 % 2 !== flower2 % 2;
}

function lovefunc(flower1, flower2){
  return (flower1 + flower2) % 2 === 1
}
profile
it's me!:)

0개의 댓글