-4
// Create a function named 'containsBool'that accepts an 

// array as a parameter.

// You can remove the comments and use the following array: 

// myArray = ['Wednesday',23,false];

// Create a for / in loop inside the function that iterates 

// through the items in the array.

// In the loop, check each array item for 'typeof' data.

// If the array contains Boolean data, return true.

// Likewise, if the array does not contain Boolean data, return false.

// Call the function and log the returned Boolean to the console.

Here is my code, I don't know where it doesn't work:

var myArray = ['Wednesday', 23, true];
function containsBool(checkBool) {
  for (g in checkBool) {
    if (typeof checkBool[g] === 'boolean') {
      return true;
    } else {
      return false;
    }
  }
}

console.log(containsBool(myArray));
connexo
  • 53,704
  • 14
  • 91
  • 128
Yangyang Cui
  • 1
  • 1
  • 5
  • `for...in` is not made to iterate over arrays. Use `for...of`. – Lucas Feb 18 '18 at 19:07
  • [Don't use `for…in` enumerations on arrays!](https://stackoverflow.com/q/500504/1048572) – Bergi Feb 18 '18 at 19:08
  • 2
    You must not `return false` when you have met the first (any) non-boolean value, you must do it when you have met none in the whole array - after the loop. – Bergi Feb 18 '18 at 19:08
  • @Bergi *// Create a for / in loop inside the function that iterates // through the items in the array.* The assignment asks him explicitly to use `for...in`. – connexo Feb 18 '18 at 19:10
  • @connexo Oh well, I didn't read that, but it's wrong nonetheless. The assignment explicitly asking for this can only be seen as an indication on the quality of the course… – Bergi Feb 18 '18 at 21:01
  • Thank you, Bergi. I was wondering why it works when the 'return false' outside the 'for' loop? Then I saw your comments. Thank you for sharing the link. – Yangyang Cui Feb 19 '18 at 15:53

3 Answers3

1

If the problem statement is not too specific to the implementation(for loop or whatever...) then this does the same job:

['Wednesday', 23, true].some((x)=> typeof x === 'boolean')
Sidtharthan
  • 197
  • 9
  • var myArray2 = ['Wednesday', 23, false]; function checkBoolean2(boolCheck) { if(boolCheck.some((x)=> typeof x === 'boolean')){ return true; } else { return false; } } console.log(checkBoolean2(myArray2)); – Yangyang Cui Feb 19 '18 at 16:17
-1

You need to be aware that return ends the for...in loop. It's okay to end it early if you found a boolean. If the loop never finds a Boolean, it reaches return false.

Please note that to iterate over an array, you need to use for...of, not for...in.

const myArray = ['Wednesday', 23, true];
function containsBool(checkBool) {
  for (arrayEntry of checkBool) {
    if (typeof arrayEntry === 'boolean') {
      return true;
    } 
  }
  return false;
}

console.log(containsBool(myArray));

For a more modern, ES6 version, you can use Array.prototype.some:

const myArray = ['Wednesday', 23, true];

console.log(myArray.some(entry => typeof entry === 'boolean'));
connexo
  • 53,704
  • 14
  • 91
  • 128
  • The person -1ing, please explain. This is exactly what the assignment asks OP to do. – connexo Feb 18 '18 at 19:18
  • Thank you very much! Your code really helps me solve this problem. I was wondering why it works when the 'return false' outside the 'for' loop? – Yangyang Cui Feb 19 '18 at 15:39
  • Then I saw "You must not return false when you have met the first (any) non-boolean value, you must do it when you have met none in the whole array - after the loop. – Bergi " – Yangyang Cui Feb 19 '18 at 15:48
  • @Bergi I think you might have downvoted for the lack of correction of OP using for...in on arrays. I changed that. – connexo Mar 05 '20 at 16:48
-1

This should do the job:

var someArray = ['Wednesday', 23, true];
function checkBoolean() {

    for (i = 0; i < someArray.length; i++) {
        if (typeof someArray[i] === "boolean") {
            return true;
        }
    }
}

console.log(checkBoolean(someArray));

In your case, should not use for...in loops to loop over the array. (Read this why.) You also should look at your else statement. If the entry to check is not an boolean (which happens twice), the function will return false. return stop executing the function so the loop gets executed only once.

CodeF0x
  • 2,624
  • 6
  • 17
  • 28