# Difference between two loops

Stack Overflow Asked by Mészáros Zoltán on January 12, 2021

The code below is the correct, working solution to an exercise I had to work out. I am wondering why my solution did not work.

 for (var i = contacts.length; i > 0; i--) {

Why it did not do the same just reversed direction?

for (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName == name){
if (contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
} else{
return "No such property";
}
}
}
return "No such contact";


let say i want the last element from contact array

let contact = ['mo','so','do'];
console.log(contact[length])


It won't show a console. Y ? Because To get a last array. You always need to -1 from the array.length

Answered by Khant on January 12, 2021

The two loops have different ranges.

If contacts.length had equalled 4, then i would have taken on these values:

console.log('ascending loop');
for (var i = 0; i < 4; i++) {console.log(i);}
console.log('descending loop');
for (var i = 4; i > 0; i--) {console.log(i);}

Answered by JMP on January 12, 2021

There are multiple problems in your code.

• the first problem is that you start i = contacts.length and as you know there is no element in the array at the array length position because arrays go from 0 to array.length-1.
the solution for that problem is var i = contacts.length - 1.
• the second problem is that i never goes to zero because your stop condition is i > 0 then you never reach the first element of the array.
the solution is changing the stop condition to i >= 0

Answered by dahan raz on January 12, 2021

On the answer, the for counter go like 0 ... n, and on your for loop, the counter goes like n ... 1.

So, on your code, the index never is 0

Answered by Zinho on January 12, 2021

## Related Questions

### Sort DOM elements based on the sorting of the array elements

2  Asked on January 25, 2021 by proseller

### Serialise array as single XML element in C#

1  Asked on January 25, 2021 by david-poxon

### How to Create a Function to extract data from a JSON file

1  Asked on January 25, 2021 by ineedhelp

### How can I convert a list of strings (each entry is three numbers separated by spaces) to an array of numbers?

1  Asked on January 25, 2021 by allison-mueller

### SQL create aggregated result from 2 queries

2  Asked on January 25, 2021 by sudhishkr

### Is there any input that would cause getline to fail in C++?

1  Asked on January 25, 2021

### Mongoose : find document with subdocument filter

1  Asked on January 25, 2021 by temp_user

### problems with @binding between structs

1  Asked on January 24, 2021 by mateus-neves

### Xamarin.Forms – How to copy Label and size with Measure()? Trying to auto size font. (Head scratcher!)

1  Asked on January 24, 2021 by aormsby

### Azure search not behaving as expected for dashes

0  Asked on January 24, 2021 by johnson

### Testing Saga using Jest that have other module dependencies

0  Asked on January 24, 2021 by mzafer

1  Asked on January 24, 2021 by musaab

### mysql join 3 table but the “COUNT” be duplicate

1  Asked on January 24, 2021 by faisal-budiman

### FTPS failing, but only on some commands on some machines

1  Asked on January 24, 2021 by watyf

### Keywords in python that evaluate to themselves

1  Asked on January 24, 2021 by sawan

### Differently displayed colors on chart.js

3  Asked on January 24, 2021 by leos-literak

### condition componentWillUnmount() in reactjs

1  Asked on January 24, 2021 by shekar

### Automapper Null on Dependency Injection Using [Inject] Attribute

2  Asked on January 24, 2021

### Vue Js Dynamic Class Binding Not Updating

2  Asked on January 24, 2021 by niall-parker

### How to use web worker inside a for loop in javascript?

1  Asked on January 24, 2021 by mex