Have a next task
Create a function that always returns True/true for every item in a given list. However, if an element is the word 'flick', switch to always returning the opposite boolean value.
Examples
['codewars', 'flick', 'code', 'wars'] // ➞ [True, False, False, False]
['flick', 'chocolate', 'adventure', 'sunshine'] // ➞ [False, False, False, False]
['bicycle', 'jarmony', 'flick', 'sheep', 'flick'] // ➞ [True, True, False, False, True]
["flick", "flick", "flick", "flick", "flick"]) // ➞ [false, true, false, true, false]
How I understand problem statment.
If an element in the array is 'flick', it toggles a switch. 'flick' may turn the switch to false, and the elements after it will follow this state. If the next 'flick' is encountered, it toggles the switch again to true, and the following elements will follow that state, and so on.
Here is how I solved this task.
function flickSwitch(arr) {
let flag = true
let array = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i].toLowerCase() == 'flick') {
flag = false;
array.push(flag);
} else {
array.push(flag);
}
}
return array;
}
console.log(flickSwitch(["codewars", "flick", "code", "wars"]));
And most of the tests passed successfully.
But if there are two 'flick' elements next to each other in the array, like ["codewars", "flick", "flick", "wars"], my solution breaks.
How can I solve this problem? Please help me!
flagback totruewhen it wasfalse?*