2

I'm trying to return all array numbers as negative numbers (* -1); I'm stuck. Please help!

function makeListNegative (array){
  for(i=0; i <array.length; i++);
    return i * -1;
  }

var negativeList = makeListNegative([7, 2, 3, 4]);
console.log(negativeList);

This function only returns that last number in the array as -4. I would like ALL list numbers to be displayed.

7 Answers 7

3

You need to map the values to negative values.

function makeListNegative(array) {
  return array.map(x => x * -1);
}

var negativeList = makeListNegative([7, 2, 3, 4]);

console.log(negativeList);

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

Comments

3
function makeListNegative (array) {
    for (i = 0; i < array.length; i++); // 1
    return i * -1;                      // 2
}
  1. Your for statement iterates (in the right range with the right interval), but with the semicolon at the end of the line, it perform no operation.

  2. Then you return the negative value of i, which is the length of the array, not a value of the array or an array with all negative values.


If you like to get you traditional approach, you could push the value in every iteration to a new array and return the new array after the iteration.

function makeListNegative(array) {
    var i, l,
        temp = [];

    for (i = 0, l = array.length; i < l; i++) {
        temp.push(-array[i]);
    }

    return temp;
}

var negativeList = makeListNegative([7, 2, 3, 4]);

console.log(negativeList);

You could map the negative values with Array#map.

function makeListNegative(array) {
    return array.map(v => -v);
}

var negativeList = makeListNegative([7, 2, 3, 4]);

console.log(negativeList);

1 Comment

No need to push since the array that is passed does not need to be retained
2

Your code

  • returned every iteration
  • had a semicolon in the wrong place
  • made the index negative instead of the array item

It could have been fixed like this

function makeListNegative(array) {
  for (var i = 0; i < array.length; i++) { // make i local
    array[i] *= -1; // negate the array item
  }
  return array; // return the manipulated array
}

var negativeList = makeListNegative([7, 2, 3, 4]);
console.log(negativeList);

Alternatively use Array.map - here in standard JS - the fat arrow => is ES6+ and does not work in IE

var negativeList = [7, 2, 3, 4].map(function(num) { return -num })
console.log(negativeList);

Comments

1

You can impress your senior devs by using ES6 syntax:

const makeListNegative = array => array.map(num => -num)

const negativeList = makeListNegative([7, 2, 3, 4]);
console.log(negativeList);

And if you don't want to create a function:

const negativeList = [7, 2, 3, 4].map(num => -num);
console.log(negativeList);

If you are using Babel or Typescript in your project, you don't need to worry about IE support because your code will be transpiled to ES5 and will be supported on all browsers.

3 Comments

I was voted down a short time ago - was that perhaps you? If so, Why?
@mplungjan , it wasn't me, in fact I upvoted one of your comments while ago.
Ok no problem just you edited your answer around the same time I was voted down and we had the same answer
0

This will not work as you are returning from your for loop. Try using

Array.map()

Comments

0

You can use map to return negative values

var negativeList=  [7, 2, 3, 4].map(function(value){
  return -v;
})

Comments

0

Since everyone solved the problem using map, here is how to solve using [forEach][1],

var makeNeg = function(arr){
  arr.forEach(function(value, index){
    arr[index] = arr[index]*-1;
  });
  return arr;
}

console.log(makeNeg([7, 2, 3, 4]))

2 Comments

No need to push. The incoming array may be manipulated directly
This is mutating data which is not ideal. You want to avoid mutation when possible so your code stays "pure" and more testable.

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.