Why isn't it possible?

why the only output is 3 ? 9 is aswell less than the 10

const jumbledNums = [123, 25, 78, 5, 9];
const lessThanTen = jumbledNums.findIndex(num => {
return num < 10;
});

console.log(lessThanTen);

findIndex gives the first index found. Not any more or less. Referring to the documentation is a good sanity check. If you want to get all the matches more you’d have something like a filter/map combo or explicit forEach (i guess the functional way would use a filter and then a map).

The findIndex() method returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.

3 Likes

To both filter and index, as such since both value and index are known and we can either write back on the same array, or write to a new one. I kind of believe that this is why there is no ‘break’ in forEach. It must iterate over the entire array, start to finish, in order. Within the callback we cannot mutate either the value or the index with any effect.

Also note that we can build an object with forEach(), not just another array. If we use function as a callback we have all the trimmings that come with it, starting with, this.

We can even step through an array using remainder on our index. Even when we don’t want to store the index, we can use it in our computation and decision making. This is such a versatile tool!

One off topic question, with regard to filter(), how does one make use of the index, and how do we return that along with the filtered data? I suppose maybe not enough time has been spent on the problem but I come up empty at each attempt, hence forEach() is my go to if I need the indices of my results. The answer may be a double palm, but I can live with that if you care to indulge me.

1 Like

@mtf yea forEach would be my go to (no pun intended). But if I were put in a place where it’d have to be all pure functions I’d do something along the lines of the code below (please forgive the roughness, I never write fp)… and I mispoke on the order of operations as the filter would lose index information on the first go!

function createTuple(value, index) {
  return [value, index];
}

function filterEvenValues(tuple) {
  return tuple[0] % 2 === 0;

const originalArray = [1, 4, 9, 16];

const mapped = originalArray.map(createTuple);
const filtered = mapped.filter(filterEvenValues);

const oneLine = originalArray.map(createTuple).filter(filterEvenValues);

and of course this only gives the filtered tuple pairs of value idx, which is not the original problem.

1 Like

FTR, I wasn’t trying to put you on the spot, not in the least. It really was an honest question, at the end. As for the comments, that was just interjection, and banter is the better part of how we work things out, if people are willing to engage. Thanks for response. Now off to read your code!

2 Likes

Yeah, I know that, too. You answered that question. The OP is a new learner with a lot of questions so my intention was to toss in this information just in case they ask next, “How do I find all the indexes?”.

Proactive? Preemptive? Yeah, I guess. Hope the OP gets some added value, is the net concern.

3 Likes

@ariannazer: Sorry to bend your mind. Well, not real sorry, just feeling where you might be right now and how you might feel. Hopefully, confused. That is part of the process of learning. Without confusion we have no way of knowing if we’ve cleared the fog, or not. The key is to stop our progress and delve deeper rather than push on.

Every concept is important, and as boring as it can seem, there is always something we don’t see at first, second, or third time glance. There are secrets buried all over the language. Not on purpose, but by nature. It’s there for us to glean. Nothing is hidden from us. Take comfort in that and take your time climbing this hill. It’s not a race.

And now for something completely different…

 > a = Object.fromEntries([['a', 'z'], ['b', 'y'], ['c', 'x']])
<- {a: 'z', b: 'y', c: 'x'}
 > Object.keys(a).forEach(function(x){this[x]=this[x].toUpperCase()}, a)
<- undefined
 > a
<- {a: 'Z', b: 'Y', c: 'X'}
 > 

That is what awaits you. Forget about it, now, just expect it in the future.

2 Likes

wow there was i fight here when i was gone , (when you sent this messages it was 5am in here and after that i went to summer school until 16 (in time of here)

thanks , not problem

ill be glad to learn it in the future ( in the code i didnt know the .fromEntries and .keys , but i think it will be a easy thing when i reached it)

2 Likes

Don’t try to glean anything from that example. It will surface when you learn about objects and arrays and their methods. Give it a pass for now and resume your studies.

You are learning how to use the JavaScript console of your browser. Play with it and practice testing your lesson code so you get a better feel for how we can use the console to test code snippets.

1 Like

ok , thanks for all the helps you gave/give

1 Like

I wouldn’t call it a fight. Just a back and forth, in my mind a healthy one.

The goal was united: to provide a good learning environment.

3 Likes