0

I have two arrays with same length. I want to get an element from one array and continue adding its value (+1) to each other elements until value is 0.

Here is my code:

const update = (source, target, index) => {
    const keys = Object.keys(source)
    let value = source[target][index]
    source[target][index] = 0
    while (value--) {
        source[target][index++]++
        if (index === source[target].length) {
            index = 0
            target = keys[(keys.indexOf(target) + 1) % keys.length]
        }
    }
    return source
}

console.log(update({a: [0, 0, 0, 8, 0], b: [0, 0, 0, 0, 0]}, 'a', 3))

answer: { a: [ 1, 0, 0, 1, 1 ], b: [ 1, 1, 1, 1, 1 ] }

So what it does is that;

  1. it takes index 3 of Array a which is 8 --> a[3] became 0

  2. continue adding (+1) to itself and other elements of both arrays until a[3] is over.

But here is the challenge, I want to pass by the last element of other array(could be array a or b) and never add +1. So my answer should be:

answer: { a: [ 1, 1, 0, 1, 1 ], b: [ 1, 1, 1, 1, 0 ] } --> last element of b not changed!

4
  • would update({a: [0, 0, 0, 8, 0], b: [13, 0, 0, 0, 0]}, 'a', 6) take 13? Commented Mar 8, 2022 at 20:21
  • No! since a.length = 5. But if you want to use 13 then u should call update({a: [0, 0, 0, 8, 0], b: [13, 0, 0, 0, 0]}, 'b', 0) Commented Mar 8, 2022 at 20:24
  • I don't understand how you get the output from the input. Commented Mar 8, 2022 at 20:48
  • Which one? The first answer has the code there Commented Mar 8, 2022 at 21:02

1 Answer 1

1

const update = (source, target_param, index_param) => {
    const keys = Object.keys(source)
    let target = target_param
    let index  = index_param
    let value = source[target][index]
    source[target][index] = 0
    while (value--) {
        source[target][index++]++
        if (index === source[target].length || target !== target_param && index === source[target].length - 1) {
            index = 0
            target = keys[(keys.indexOf(target) + 1) % keys.length]
        }
    }
    return source
}

console.log(update({a: [0, 0, 0, 8, 0], b: [0, 0, 0, 0, 0]}, 'a', 3))

The most important change being the condition (index === source[target].length || target !== target_param && index === source[target].length - 1) and not mutating the function params.

I strongly suggest never mutating the function parameters.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, What do you mean by saying (never mutating the function parameters)? Any mistake in my code?
Not per se. I mean not to change values of the variables that are parameters of the function (those in parentheses at the function declaration). It is an opinion, but I strongly recommend it. BTW you can +1 my ans and mark it as solving if it is solving.

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.