indexOf(Array) Help

indexOf(Array) Help
0.0 0


Guys, i’m doing the counting cards exercise and i know i’m supposed to do with else-if or switch but i’m curious on how to do it with array. Like, if the card belongs to that array i calc a new count, but the code is not working, what’s wrong?

 var count = 0;

function cc(card) {
  // Only change code below this line
 if (arraypositive.indexOf(card)){
   count = count + 1;
 else if (arrayneutral.indexOf(card)){
 else {
   count = count - 1;
 if(count > 0){  
  return count+' Bet';
   return count+' Hold';
  // Only change code above this line

// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(4); cc(5); cc(6);


I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.


indexOf returns a number. It returns the index of the item if it’s found in the array and -1 if the item is not in the array. If the item is found at the first position of the array, then indexOf will return 0, which is “falsey”. If the item is not in the array it will return -1, which is “truthy”.


arraypositive.indexOf(card) will return the index of the card in the array if found and -1 if it is not in the array.

Since -1 would be truthy even though no card is found, and 0 would be falsy if a card was found in index 1, you need to set your condition explicity eg

arraypositive.indexOf(card) !== -1


Ye, it worked now. So it can only return 0 / -1?


Nope. It will return the index of the item or -1

var myArr = ['a', 'b', 'c'];
myArr.indexOf('a'); // 0
myArr.indexOf('c'); // 2
myArr.indexOf('z'); // -1