0

I'm trying to get an array I've called input and loop over it.

If any values are odd numbers then they get multiplied by 2.

If any are even they get divided by 2 and are then pushed to an output array which I can then return.

So far I have this, (using cmd node):

function modify(input) {
    var output = [];
    for (i=0; i=input.length; i++) {
        if (input % 2 == 1) {
            input*2;
            output.push[input];
        }
        if (input % 2 == 0) {
            input/2;
            output.push[input];
        }
    }
    return output;
}

module.exports.modify = modify;
0

4 Answers 4

1

The biggest problem is that you never access the elements of input, just the array itself. Your if (input % 2 == 1) line checks if the array mod 2 is equal to 1. That doesn't make sense, as you can't really do math on an array.

You need to do this per-element, so the minimal change would be to use input[i] within the loop.

There are a number of other changes you can make, and some idiomatic patterns that can make the code nicer. If you're on a recent browser and have forEach or map, you can replace the loop with a construct like:

var output = input.map(function (it) {
    if (it % 2 == 1) return it * 2;
    if (it % 2 == 0) return it / 2;
});

That can still be cleaned up, as x % 2 can only ever return 0 or 1, so you can replace the second condition with an else or just assume it returned 0:

var output = input.map(function (it) {
    if (it % 2 == 1) return it * 2;
    return it / 2;
});

Because of how JS handles true and false, specifically converting numbers, you can omit the == 1 (1 is truthy), and swap the condition for a ternary:

var output = input.map(function (it) {
    return (it % 2) ? it * 2 : it / 2;
});

Since you're wrapping this into a function, if you're using map then output isn't strictly necessary, so you can do:

module.exports.modify = function (input) {
    return input.map(function (it) {
        return (it % 2) ? it * 2 : it / 2;
    });
};

If you have ES6 support (likely through the brilliant 6to5 project), you can replace the function declarations with arrow functions:

module.exports.modify = (input) => {
    return input.map((it) => {
        return (it % 2) ? it * 2 : it / 2;
    });
};

And if you want to get really academic with it, you can remove the return statements (thanks to arrow functions):

module.exports.modify = input => input.map(it => it % 2 ? it * 2 : it / 2);
Sign up to request clarification or add additional context in comments.

Comments

0

Some things I see in your code:

  • You're not getting the values out of input, and you're using the entire array instead.
  • Your for loop's terminating condition is set wrong (should be i < input.length)
  • You're not assigning the result of your operations (input*2 and input/2) back into the value. Even if you weren't referencing the array itself, you'd be inserting an unmodified value.
  • You're using brackets instead of parentheses when you call push()

Try this instead:

function modify(input) {
    var output = [];
    for (i=0; i<input.length; i++) {
        var inval = input[i];
        if (inval % 2 == 1) {
            inval = inval*2;
            output.push(inval);
        } else {
            inval = inval/2;
            output.push(inval);
        }
    }
    return output;
}

Comments

0

This is wrong:

for (i=0; i=input.length; i++) {
      if (input % 2 == 1) {

input is the entire array. You can't modulo an array by 2.

You need to check individual elements with the [] operator:

if (input[i] % 2 == 1) ...

The same applies inside the bodies of the if condition:

        input*2;
        output.push[input];

This needs to be input[i] * 2, and you also need to store that value. Right now, you have two unrelated statements: input * 2, which (if it were correct) would compute the value and discard it. Then (and again, your syntax is in correct, you need () not [] here) output.push[input] would append the original value to output.

[] is for accessing an element in an array, and () is for invoking a function. push is a function. The line could be correctly written as:

output.push(input[i] * 2)

Comments

0

A few errors here;

Your for loop condition makes sure that i is equal to the length of the array, not less than it. Unless your input array is empty the loop will never run.

for(var i = 0; i < input.length; i++)

You're checking if input (the array) is divisible by 2, not if the element inside of input is.

if(input[i] % 2 === 1)

You're also performing a math operation against the array object, not the element in the array, and you're not persisting it anywhere.

var multiplied = input[i] * 2;

You're calling the push method of the output array as an array, not as a function call.

output.push(multiplied);

Comments

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.