1var obj = {a: 5};
2var array = [obj, "string", 5]; // Must be same object
3array.indexOf(obj) !== -1 // True
1//Use something like this:
2
3function containsObject(obj, list) {
4 var i;
5 for (i = 0; i < list.length; i++) {
6 if (list[i] === obj) {
7 return true;
8 }
9 }
10
11 return false;
12}
13
14//In this case, containsObject(car4, carBrands) is true. Remove the carBrands.push(car4); call and it will return false instead. If you later expand to using objects to store these other car objects instead of using arrays, you could use something like this instead:
15
16function containsObject(obj, list) {
17 var x;
18 for (x in list) {
19 if (list.hasOwnProperty(x) && list[x] === obj) {
20 return true;
21 }
22 }
23
24 return false;
25}
1//We will make a search box kind of thing. It will ask the user to enter a fruit and we will make a list fruits. If the user input is in list, then print you win
2
3//Make a list for fruits
4var FRUITS = ["Apple","Bananna","Mango","Kiwi"];
5
6//User input
7var User_Input = prompt("Enter a fruit");
8
9//Variable to check if user_input is in fruit
10var UserInFruit = FRUITS.contains(User_Input);
11
12//If staatement
13if (UserInFruit = true){
14 print("You win")
15}