0

There is an array of 10 students below in the code part. I wanted to display that as

  1. Aaran
  2. Aaren
  3. Aarez ...up to 10

but I am unable to achieve this using for a loop as the index starts from 0 but if I start it from 1 then it skips the first name.

Here is my code below:

var stu = ["Aaran", "Aaren", "Aarez", "Aarman", "Aaron", "Aaron-James", "Aarron", "Aaryan", "Aaryn", "Aayan"]

// 1. counting the students names:
var ln = stu.length;

//2. showing the students names using for loop with numbering:
for (var i = 1; i < ln; i++) {
  document.write(i + " : " + stu[i] + "<br>")
}

It gives me numbers like 1, 2, 3 but it skips the first item of the array. So how can I achieve this without skipping?

gkulshrestha
  • 795
  • 1
  • 5
  • 11
Brove
  • 1
  • 1
  • [Why is document.write considered bad practice?](https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) – isherwood Oct 13 '20 at 20:54
  • @riyoz, answers go down there. – isherwood Oct 13 '20 at 20:55
  • I would look at [map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) and [innerHTML()](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML). Keep in mind that index is zero-based. – isherwood Oct 13 '20 at 20:59
  • You have the variable `i` which is starting from 0. So you need to always print `i+1`. If you do not understand any part of this let me know. – sabbir.alam Oct 13 '20 at 20:59

5 Answers5

1

Change i=0 and i+1 at write.

    var stu =  ["Aaran", "Aaren", "Aarez", "Aarman", "Aaron", "Aaron-James", "Aarron", "Aaryan", "Aaryn", "Aayan"]
    for (var i = 0; i<stu.length; i++){ 
        document.write(  i+1 + " : " + stu[i] + "<br>")
    }
HoldOffHunger
  • 10,963
  • 6
  • 53
  • 100
Valentin
  • 121
  • 2
  • 6
0

The variable "i" should be 0, the first index in arrays always start with 0, so it should look like this:

for (var i = 0; i<ln; i++) {
    document.write(  i + 1 + " : " + stu[i] + "<br>")
}
Garsivirus
  • 93
  • 8
0

You can also try iterating array like this:

for(let i in stu){
     document.write((i+1) + " : " + stu[i] + "<br>");
}

Ketan Ramteke
  • 7,842
  • 2
  • 13
  • 30
0

You can use forEach, its easier to work with. JavaScript Array forEach() method

The array.forEach access directly the array elements, so you dont need to use a for loop accessing them by their indexes.

Exemple:

var stu = ["Aaran", "Aaren", "Aarez", "Aarman", "Aaron", "Aaron-James", "Aarron", "Aaryan", "Aaryn", "Aayan"]

stu.forEach((element, index) => {
  document.write(index + " : " + element + "<br>");
});


document.write('<br>If you want to show it as 1 to 10, them just add 1 to the "index" variable in the "document.write". Be sure to separate it in a new scope to prevent errors. <br><br>');

stu.forEach((element, index) => {
  document.write((index + 1) + " : " + element + "<br>");
});
Luís HNrique
  • 140
  • 11
-1

This should work

for(let i = 0; i<stu.length; i++){
     document.write((i+1) + " : " + stu[i] + "<br>");
}
riyoz
  • 361
  • 3
  • 14
Tommaso
  • 124
  • 1
  • 9