2

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!

1
  • "If the next 'flick' is encountered, it toggles the switch again to true": this is a correct understanding, but where in the loop do you switch flag back to true when it was false?* Commented Jun 7 at 11:46

3 Answers 3

1

The problem with the code you wrote is that when 'flick' is seen you set the flag to false but you want toggle so you need to replace `flag = !flag`
boolean = !boolean always stores the opposite of the previous state.

Modify your code

function flickSwitch(arr) {
    let flag = true
    let array = [];
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].toLowerCase() == 'flick') {
            flag = !flag;  // just here is changed
            array.push(flag);
        } else {
            array.push(flag);
        }
    }
    return array;
}

console.log(flickSwitch(["codewars", "flick", "code", "wars"]));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, that's right, it was very helpful — I really appreciate it
1
  • You can set flick as your second function's argument (that way you can also decide which initial flick state to use, in order to inverse the logic if necessary)
  • Toggle the flick if str === "flick"
  • You could use Array.prototype.map() in order to return a new Array with the boolean values

const flickSwitch = (arr, flick = true) => {
  return arr.map(str => {
    if (str === "flick") flick = !flick;
    return flick;
  });
};

console.log(flickSwitch(['codewars', 'flick', 'code', 'wars'])) // [True, False, False, False]
console.log(flickSwitch(['flick', 'chocolate', 'adventure', 'sunshine'])) // [False, False, False, False]
console.log(flickSwitch(['bicycle', 'jarmony', 'flick', 'sheep', 'flick'])) // [True, True, False, False, True]
console.log(flickSwitch(["flick", "flick", "flick", "flick", "flick"])) // [false, true, false, true, false]

Or the shorter, with Arrow function's implicit return:

const flickSwitch = (arr, f = true) => arr.map(s =>  s === "flick" ? f = !f : f);

console.log(flickSwitch(['codewars', 'flick', 'code', 'wars'])) // [True, False, False, False]
console.log(flickSwitch(['flick', 'chocolate', 'adventure', 'sunshine'])) // [False, False, False, False]
console.log(flickSwitch(['bicycle', 'jarmony', 'flick', 'sheep', 'flick'])) // [True, True, False, False, True]
console.log(flickSwitch(["flick", "flick", "flick", "flick", "flick"])) // [false, true, false, true, false]

(Fun offtopic) Code-golfing it would look like:

const flickSwitch=(a,f=0)=>a.map(s=>!(s=="flick"?f^=1:f));

2 Comments

Thank you so much, that's right, it was very helpful — I really appreciate it
@FollowMagnus you're very welcome, happy coding
0

You have to invert the flag but not just change to false

And do not put same actions inside if() and else, the code will be shorter.

function flickSwitch(arr) {
    let flag = true
    let array = [];
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].toLowerCase() == 'flick')
            flag = !flag;
        array.push(flag);
    }
    return array;
}

console.log(flickSwitch(["codewars", "flick", "code", "flick"]));

1 Comment

Thank you so much, that's right, it was very helpful — I really appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.