Sequential Search - calling the right variable from an array within a loop statement

I’m very new to JavaScript and programming in general - I’m following along in a book, but try to take their code to the next step. However, I’m running into an error.

This program is designed to teach me sequential search and the general gist of the program is to take input from a user and store it using four variables tied to arrays - partN[i], description[i], itemCost[i], and quantity[i]. Most of the code works correctly and I typically test our three to four items, and used document.write statements to print it on the screen so I could see if that part worked correctly, which it does.

The last part of the program, and where I’m having the issue is posted below. Basically once the information is stored in the Arrays, it allows the user to specify a part number to search for and if it is found it will display information from the four variables on that part. However, it doesn’t matter what I input into “searchPt”, it always returns the value “Item Found” even when it shouldn’t be found. What am I doing wrong?

// Now find the item in inventory

	searchPt = prompt ("What is the item number you want to see?");
	foundPart = 0; // this is the flag to alert you if found

	for (i = 0; i < totalNumber; i++)
	   {
		if (partN[i] = searchPt)
			
		{
		document.write ("<br>Item found");
		foundPart = 1;		
		break;
		}
	}

	if (foundPart ==0)
	{
		document.write("<br>Item not found");
	}

Ooooh that’s a nasty one!

= must be ==

:slight_smile:

Also, instead of 0 and 1, it’s better to use true and false in this case. 0 and 1 works just as well, but true and false are more clear.

Thank you so much! I guess the book had an error on this one because I copied it directly :slight_smile: but once you said that it made perfect sense. I also appreciate the tip on true/false instead of 0/1

Recommended practice is to use === for comparisons rather than == so that you don’t end up with fields being accidentally converted to different types. It also helps avoid the error of accidentally leaving off a =

Thanks for the extra tip. I’m an IT recruiter by day and had a client use the === versus == as one of their screening questions. Didn’t know what it meant until now :slight_smile:

What about object.hasOwnProperty?

What about it?

Misunderstood… thought OP was looking for key. But wouldn’t indexOf() be more efficient than looping through the object and comparing?

That only works on 1D arrays, this is a 2D array (an array with arrays nested in it).
If it were a 1D array than yes indexOf would be a more compact way of writing it but performance wise the same since indexOf also loops under the hood, same as the OP’s code :slight_smile:

As a side note, if you still support oldIE (IE < 9), you can’t use Array.indexOf, or you at least have to define it yourself (there’s a polyfill on the MDN page, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)

Thanks for the extra tip. I’m an IT recruiter by day and had a client use the === versus == as one of their screening questions. Didn’t know what it meant until now

It’s good advice, but will work best if you are certain you know what to expect of your values. An example of a problem:
user types in a 5 into an HTML form input
you want to check if it’s a 5

if (theinput.value() === 5)
… nope.

In this case, HTML form elements’ values are strings, meaning you either compare with another string or try to change the value to some number type first.

In Python for example, === may check to see if the two values are actually the same place in memory (meaning they must be, in fact, the same thing).

I vaguely remember reading a hilarious rant about PHP about a special circumstance where comparisons go horribly wrong for a non-logical reason…

Wasn’t this, was it?

I’m not certain, I seem to recall some paragraph about cmp in particular but it could have also been in the PHP, Fractal of a Bad Design (a classic, getting pretty old by now) or another place.