알고리즘 63 - Thinkful - Logic Drills: Traffic light

tamagoyakii·2021년 10월 20일
0

알고리즘

목록 보기
63/89

Q.

You're writing code to control your town's traffic lights. You need a function to handle each change from green, to yellow, to red, and then to green again.

Complete the function that takes a string as an argument representing the current state of the light and returns a string representing the state the light should change to.

For example, update_light('green') should return 'yellow'.

Note: Use "" instead of '' in C++.

A)

function updateLight(current) {
  const lights = ['green', 'yellow', 'red', 'green'];
  for (el in lights) {
    if (lights[el] === current)
      return lights[1 + parseInt(el)]
  }
}

0개의 댓글