Challenge 13 Megathread

it’s tricky. In your loop you used “in”

for (let I in a)

you are accessing the index ex. 0, 1, 2 …

if you want to access de object inside the array you have to use “of”

1 Like

const checkAllGauges = (gaugeList) => {
let checkGauge = [];
for (let i = 0; i < gaugeList.length; i++) {
if (gaugeList[i].current >= gaugeList.min | gaugeList[i].current <= gaugeList.max) {
return true;
} else {
return false;
}
}
return checkGauge;
};

After trying to figure this out for an hour. I am stuck. I am passing the first test but I am failing the second. It isn’t returning the correct result. Any suggestions?

Start with what would make it false because that should end the loop. If they’re all true then return true after the loop. You also setting i but not using it to index.

i is the array index, not the object.

You call an element in an array using its index with the array[index] construction.

This would just end when it finds a true value. They do want you to return one value though its just false if ones bad but true if they’re all good.

don’t for get to add ; after the return

for (......) {
    if (.....) {
         return false;
    }
}

don’t return an array for result and when finding a first false, break the loop.
I hope it works.

const checkAllGauges = (gaugeList) => {
for (let i = 0; i < gaugeList.length; i++) {
This part deleted by the moderators.
}
}
};

I did this but just one test is passing. The test that should return the proper value is failing. Any suggestions?

cjo353

15m

Hi b-javadi,

I removed the new array, it comes back with the same result. It states that it passed the first test and the function returned a boolean, however, it failed the second test as the code ran but did not produce the correct result. it suggests Hint: make sure you are checking all the gauges?

Expected: true

Actual: false

try this one

Please do not share your correct code. This part deleted by moderators since the part is against blog rules.

I believe where you went wrong was your last if statement.

if (counter > 0) {
   return true;
} else {
   return false;
}

when you loop through booleanList, you add up how many times it validates as true. However, the condition for your final if statement is where I can see a problem. It will return true if just one gauge is within the min and max values. It should return true if ALL gauges are with min and max. It should be:

if (counter == booleanList.length) {
   return true;
} else {
   return false;
}

Thanks for your help, but unfortunately it keeps returning the wrong value and fails the second test… I am unsure why it keeps saying that. If i reverse the true and false returns it still says it did not produce the correct result…

It’s your if statement. You index gaugeList for current but not for max or min.

I linked the wrong person to my answer to you.

Hope that helps.

Hi Sonicthecronic,

I tried adding the index to the min and max. Still returns incorrect result

Your code will return true or false after the first element of your array and it will stop. But the logic should
return true if all elements are between max and min
or return false if it catches any false and no need to go on with your loop.
Return will stop the loop. So, be careful how you use the return.

Try to check if it is out of the limit and return false. No else statement. If it completes the loop without returning false, you can put return true to the out of the loop.

3 Likes

I found it! The issue. You have >= and <=

You just neeed < and >
Nevermind,. I was looking at your old code.

Hi alibas,

do you mean the if statement should have the current value between the min and max instead of using AND or OR?

I was able to easily find the solution using loops but having a hard time figuring it out using some. Do you mind sharing your code with me through PM?

How about if I put like this;

some other needed code lines.
  if(current > max || current <min) {
    return false;
  };
};
  return true;
};
1 Like