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:

1 Like

I got it, thank you very much!

It’s not working for me

Hey there,

In your if statement, you’re testing if the string 'test' is equal to the string 'fail'.

Let’s take a look at how the function works.

The check() function takes an array of strings, which is represented by the results parameter.

It then loops through results to look at each string individually. On every loop, the current string is represented by the element variable.

Use this variable in your if statement instead of the string 'test' that you were using before. It will look like this:

for (var element of results) {
    if (element === 'fail') {
        return 'failed';
    }
}

That should work. Let me know if you have any questions or encounter any other difficulty.
Ben

Hello, my results is working but it seems there is a problem and I can’t understand why.
Both of my returns are right in place.

Hey there,

Your check function has a parameter called results, but that parameter isn’t being used anywhere in the function. You are instead using testResults, which we want to use as the argument when the function is called.

A quick refresher on parameters vs arguments:

When a function is declared, a parameter can be added inside the parentheses (not all functions require parameters). This parameter is like a special variable that lives inside the function. When the function is called, whatever is passed in as an argument will behave like the parameter.

For example:

function double(num) {
    return num * 2;
}

double(5)

In the above example, num is the parameter, while 5 is the argument.

Hope this clears things up! Let me know if you have any questions!
-Ben

1 Like

Hey Ben ! Thank for your patience.
I knew the difference between the parametre and argument but it wasn’t enough.

Sometimes we need to repeat before it’s sticks. :slight_smile:
Thank you so much

1 Like

What did I do wrong?

Hey there, it looks like the testResults inside the import statement was renamed to Results. Because there isn’t anything in 'grasshopper.laboratory' named Results, this means nothing is being imported.

Try resetting the puzzle to set the names back, then try your solution again. That should solve the problem.

Hope this helps!
Ben

It did not help

This is really important!

Hey there, it looks like there is still Results with a capital R in the import statement. This is causing the issue, because it should be testResults.

To fix this, tap the reset button to change the import statement back. It’s the button that looks like a circular arrow (like the refresh button on a browser). Then try the puzzle again.

Let me know if you have any questions,
Ben