最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to create negative numbers in array by loop? - Stack Overflow

programmeradmin3浏览0评论

I'd like to know how to create an array like the following with a for loop (notice that the accepted answer includes 0 while it was not part of my requirements. I guess I should meditate on this and stop ignoring people's ments).

var arr = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

My try can't print the negative numbers in array.

for (var i = -10; i <= 10; i++) {
    arr[i] = i;
}

Result:

0,1,2,3,4,5,6,7,8,9,10

I also don't want the negative numbers of indexes in the array.

arr[-10]....arr[-9].....arr[1]...

I'd like to know how to create an array like the following with a for loop (notice that the accepted answer includes 0 while it was not part of my requirements. I guess I should meditate on this and stop ignoring people's ments).

var arr = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

My try can't print the negative numbers in array.

for (var i = -10; i <= 10; i++) {
    arr[i] = i;
}

Result:

0,1,2,3,4,5,6,7,8,9,10

I also don't want the negative numbers of indexes in the array.

arr[-10]....arr[-9].....arr[1]...
Share Improve this question edited Sep 13, 2015 at 5:48 user1636522 asked Sep 13, 2015 at 4:45 AllenAllen 171 silver badge8 bronze badges 3
  • 2 You do not have a 0 in the desired final array. Is this intentional? – intcreator Commented Sep 13, 2015 at 5:04
  • Answer the ments next time please Allen. Thanks. – user1636522 Commented Sep 13, 2015 at 5:33
  • If you care about performances read this ment: stackoverflow./questions/32546391/…. – user1636522 Commented Sep 13, 2015 at 6:47
Add a ment  | 

6 Answers 6

Reset to default 7

If you push to the array, you don't have to state the index:

var i, arr = [];
for (i = -10; i <= 10; i++) {
    arr.push(i);
}

If you need to skip 0:

for (i = -10; i <= 10; i++) {
    i !== 0 && arr.push(i);
}

Try this way:

for (int i = 0; i <= 20; i++)
{
    arr [i] = i - 10;
}

Indexes in an array always starts from 0 and can't be negative so if you need negative values at positive indexes some putations or translation of values must take place.

var arr = [];
for (var j = -10; j <= 10; j++)
    arr[arr.length] = j;

As I don't see 0 in question:

var arr = [];
for (var j = 1; j <= 10; j++) {
	arr.push(j);
	arr.unshift((j) * (-1));	
}
document.getElementById('data').innerHTML = JSON.stringify(arr);
<div id="data"></div>

In order to provide the desired sequence of -10 to -1 then 1 to 10 without the 0, consider the following:

var arr = [];
for(var i = -10; i < 0; i++) arr[i + 10] = i;
for(var i = 1; i <= 10; i++) arr[i + 10] = i;

If you want to use the same loop with a different range, just define a variable to replace 10 every time you see it in the code given above.

Use this :

var array = new Array();
var arrayIndex = 0;

for(var i=-10; i<=10;i++){
  array[arrayIndex++] = i;
}

Array indexes start at 0 and cannot be negative.

Explanation:

The first line declares the array.

The second line declares a variable named arrayIndex.

The for loop iterates through all the numbers ranging between -10 to 10 and assigning all those values from array[0] to array[20]

发布评论

评论列表(0)

  1. 暂无评论