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++.
void main() { test("current light is green", () { expect(updateLight("green"), equals("yellow")); }); }
내답:
String updateLight(current) { if (current == "green"){ return "yellow"; } else if (current == "yellow"){ return "red"; } else { return "green"; } }
String updateLight(current) {
switch (current) {
case 'green': return 'yellow';
case 'yellow': return 'red';
case 'red': return 'green';
}
}
String updateLight(current) {
return {"green": "yellow", "yellow": "red", "red": "green"}[current];
}
String updateLight(current) {
return current == "red" ? "green" : current == "green" ? "yellow" : "red";
}