All or Nothing explainer

The aim of this puzzle: Complete the check() function so that it returns the number of tests, or false if any of the tests fail.

Walkthrough of the solution: The testResults variable is an array of strings. Each string is either 'pass' or 'fail'. For example, it could look like ['pass', 'fail', 'pass'], or ['pass', 'pass', 'pass', 'pass'].

Once the check() function is completed, it should be able to return 'failed' in the 1st example, or 4 in the 2nd example.

To start, the function uses 1 parameter, results, which will store the array of tests. To look at each test result, we can use a for loop. for (let test of results) {...}.

Inside of the loop, we need to check if the test is 'pass' or 'fail'. If it’s 'pass', we can go on to checking the next item. If it’s 'fail', then we know the function should just return 'failed'. Inside the loop, add

if (test === 'fail') {
  return 'failed';
}

If all of the items in the loop are 'pass', then we can return the total number of tests passed, which is just the length of the results array. After the loop, but still inside of the function declaration, return results.length. You could also add + ' tests passed' so that it’s clearer what the number means.

I have added + ’ tests passed.’ to return results.length but I keep getting a message saying that I need to add + ’ tests passed.’ when I’ve already done it. What am I doing wrong? The code looks like it’s printing correctly but it won’t let me pass

Hi @Bre_Anna_Bailey,

You may be adding the return statement in the wrong place (it’s an easy mistake to make). Doublecheck that your return statement is after the for loop’s block {}. Otherwise, the function will always return on the first iteration of the loop, and never get a chance to check the whole array.

If you don’t think that’s the issue, try posting a screenshot of your code here and I’d be happy to try and debug it.

H :slight_smile:


From what I can tell I think I’m doing it correctly

I also tried making the second return statement an if statement to see if that would work but I got the same result

@Bre_Anna_Bailey — thanks for sharing!

The second return statement is inside the for loop, rather than after.

This is a really easy error to make (that professional programmers often do). A good way to check that your code is in the correct place is to match up code blocks (in this case curly brackets {}) from the bottom up, matching every bottom bracket with a top bracket.

Looking at the bottom of your code we can see the bottom-most curly bracket is just above the console.log() statement. This corresponds to see the first opening bracket of the function declaration. That means this bottom-most curly bracket is the end of the function declaration.

If we go up one line from the bottom, we can see another curly bracket — what is this the closing bracket for? That is the closing bracket for the for loop.

That means the return statement should come after that curly bracket, but before the final curly bracket that closes the function declaration. Right now your code will return after looking at the first element in the array; but we want it to keep looping if the test is not ‘fail’.

Your function should look something like this:

function check(results) {
   for (var test of results) {
      if (test === 'fail') {
       return 'failed'
      }
   }
  return results.length +' tests passed.'
}

I hope this helps a little!

H :slight_smile:

I got it, thank you very much!