Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Iterating through an Array

what's wrong in my code

var inStock = [ 'apples', 'eggs', 'milk', 'cookies', 'cheese', 'bread', 'lettuce', 'carrot', 'broccoli', 'pizza', 'potato', 'crackers', 'onion', 'tofu', 'frozen dinner', 'cucumber'];

for(var i=0; i<=inStock.length; i+=1){

 var search = prompt("What item u want?");

  if(search === inStock[i]){
    print(search);

}else{

 alert("no item like"+search); 
}


function print(message) {
  document.write( '<p>' + message + 'is there in our shop'+'</p>');
}
script.js
var temperatures = [100,90,99,80,70,65,30,10];
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

EDIT: Made code more viewable. - Dane E. Parchment Jr. (Moderator)

2 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

I believe that you need to subtract 1 from inStock.length.

When you call the length of a an array it will give you the actual number of elements, lets say 5.

However, based on array math, and computer science in general arrays start at the number 0, so here is a comparison of the ways that they would count if you start your count at 0:

  • Counting via inStock.length: 0, 1, 2, 3, 4, 5
  • How arrays count: 0, 1, 2, 3, 4

So as you can see in your loop you would be referencing a number 5, which does not exist in the array. So if you decrement your inStock.length by 1 you would get the desired results.

for(var i=0; i<=inStock.length-1; i+=1){
      //...do stuff here
}

Another way you can solve this without the extra math, is to change the less-than-or-equal-to operator to simply just less-than, this way it will only loop up to the value 1 less than the length of the array because the array length cannot be less than itself:

for(var i=0; i<inStock.length; i+=1){
      //...do stuff here
}

could you please write that piece of code

Dane Parchment
Dane Parchment
Treehouse Moderator 11,077 Points

In the conditions for the for loop simply subtract 1 from inStock.length like so:

for(var i=0; i<=inStock.length-1; i+=1){
      //...do stuff here
}

Another way you can solve this without the extra math, is to change the less-than-or-equal-to operator to simply just less-than, this way it will only loop up to the value 1 less than the length of the array because the array length cannot be less than itself:

for(var i=0; i<inStock.length; i+=1){
      //...do stuff here
}

I have added this code to my original answer.