admin管理员组文章数量:1353132
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.
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.
Share Improve this question asked Aug 13, 2018 at 16:52 Engie TEngie T 211 silver badge2 bronze badges7 Answers
Reset to default 3You 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);
function makeListNegative (array) { for (i = 0; i < array.length; i++); // 1 return i * -1; // 2 }
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.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);
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);
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.
This will not work as you are return
ing from your for
loop. Try using
Array.map()
You can use map to return negative values
var negativeList= [7, 2, 3, 4].map(function(value){
return -v;
})
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]))
本文标签: javascriptMake array numbers negativeStack Overflow
版权声明:本文标题:javascript - Make array numbers negative - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743902695a2558962.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论