1

On my Firefox web console, I enter the following JS

const scoreTierMins = [90, 70, 50, 30, 10, 0]; 
const pctScore = 75; 

I want to find the index of the first element in the "scoreTierMins" array that is greater than or equal to "pctScore". So I tried this

scoreTierMins.findIndex(function(el) {el >= pctScore}); 

but I get the result "-1". What else am I missing? Shouldn't the result be "1" because "70" is the first element that matches the condition?

3
  • You're missing a return. Commented Aug 31, 2020 at 17:09
  • aka: scoreTierMins.findIndex(el => el >= pctScore) Commented Aug 31, 2020 at 17:10
  • Also the result should be 0, not 1. Commented Aug 31, 2020 at 17:11

3 Answers 3

1

You aren't returning the result of the comparison. Though you put the expression el >= pctScore in the function body, without a return statement it's just an expression that does nothing and whose value is discarded. It's as if you wrote this function:

function compareElements(el) {
  el >= pctScore;
}

When you write it out it should be clear that it's not doing anything.

Instead, return the condition like so:

const scoreTierMins = [90, 70, 50, 30, 10, 0]; 
const pctScore = 75; 

let index = scoreTierMins.findIndex(function(el) {return el >= pctScore}); 
console.log(index);

Or use ES6 arrow functions to be more concise: scoreTierMins.findIndex(el => el >= pctScore) - when the body of an arrow function is just an expression, it is implicitly unwrapped to el => { return el >= pctScore }.

Sign up to request clarification or add additional context in comments.

2 Comments

Just use a lambda expression, or utilize new-lines to show WHY they need the return. Just throwing it in there does not point out their error in trying to write it all on one line.
@Mr.Polywhirl I did recommend an arrow function under the code snippet. I also do point out the error in the first paragraph before introducing the fixed code, but I took your suggestion on writing out the function in full form to show that it looks incorrect.
0

The 'findIndex callback' should return a 'boolean'. In your case, return the condition.

Comments

0

I had a similar problem searching through test of Regular expression, here the Javascript (FireFox) log:

re = new RegExp("^width\\s*[:=]|^width$","gi");
/^width\s*[:=]|^width$/gi

h = ["width : 250","Height","300","top"]
Array(4) [ "width : 250", "Height", "300", "top" ]

h.findIndex(el => re.test(el))
0
h.findIndex(el => re.test(el))
-1
h.findIndex(el => re.test(el))
0
h.findIndex(el => re.test(el))
-1

If instead of "width" I search for "height" or "width" is not the first item I always get the correct answer.

I got the correct results by eliminating the g (global) flag from the regular expression because with this flag:

RegExp objects are stateful when they have the global or sticky flags set

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.