 8kyu – Logic Drills: Traffic light

You need a function to handle each change from green, to yellow, to red, and then to green again.

Based on how the question is formulated it is suggested that you should always return the next item in an array. If it is the last item it should return the first item.

``````/**
*
* @param {'green'| 'yellow'| 'red'} current
* @returns {string}
*/
function updateLight(current) {
let lights = ['green', 'yellow', 'red'];
let nextIndex = (lights.indexOf(current) + 1) % lights.length;
return lights[nextIndex];
}``````

However as alternative you also could say: We always already know the next state, we do not need to calculate the next state. For traffic lights this system also probably will never change. Thus you can use an object to save the next states.

``````/**
*
* @param {'green'| 'yellow'| 'red'} current
* @returns {string}
*/
function updateLight2(current) {
return {
'green': 'red',
'yellow': 'green',
'red': 'yellow'
}[current];
}``````

7kyu – Sum of numerous arguments

The idea of this problem is mostly to make aware of that you can pass in an arbitrary amount of arguments into a js function.

In ES5 you would be using the `arguments` object and iterate over the array. in ES6 however we can use the cleaner spread-syntax:

``````/**
*
* @param {number[]} args
* @returns {number}
*/
function findSum(...args){
if (args.find(val => val < 0)){
return -1;
} else {
return [...arguments].reduce((total, current) => total + current);
}
}``````