1

How do i make it so that the index starts with 1 instead of 0. Also how would i make it 1st, 2ed, 3rd etc. in instead on 1 2 3 etc.

<p id = "array"></p>        
    <script>
            var car = ["Nissan Skyline R34 GTR", "Toyota Supra A80", "Mazda RX7 FD3S", "Honda NSX Type R", "Nissan 240SX", "Toyoto AE86", "Subaru Impreza WRX Sti", "Honda Civic EG6"];
            
            var text = "";
            car.forEach(script)
            document.getElementById("array").innerHTML = text;
            
            function script (value, index, array) {
              text += ("My #" + index + " choice is " + value + ". <br>");
            }
        </script>
JanamP
  • 11
  • 3

1 Answers1

2

How to change the value of the index number of an array so that it would start from 1 instead of 0

Just answering the question without asking why you want that: You can achieve it by inserting and deleting a dummy element 0.

<p id = "array"></p>        
<script>
    var car = ["", "Nissan Skyline R34 GTR", "Toyota Supra A80", "Mazda RX7 FD3S", "Honda NSX Type R", "Nissan 240SX", "Toyoto AE86", "Subaru Impreza WRX Sti", "Honda Civic EG6"];
    delete car[0]
    var text = "";
    car.forEach(script)
        document.getElementById("array").innerHTML = text;

    function script(value, index, array)
    { text += ("My #" + index + " choice is " + value + ". <br>"); }
</script>
Armali
  • 18,255
  • 14
  • 57
  • 171