FAQ: Iterators - Iterator Documentation

word is the parameter of the callback function, so you can name the parameter anything you like. but word is a sensible choice, given the .every method will loop over your array, and pass each element/individual word as argument.

1 Like

Who else wants to punch JavaScript in its’ face? I swear I start to grasp one part only to come and feel like a complete incompetent scrub in the next. How? How do I get better at this? Every job interview I go to the tech assessment ALWAYS has JavaScript at this rate I simply don’t see how I’ll ever land a job. Thanks for coming to my TED Talk. Sincerely, I hate JavaScript with a deep burning passion.

Practice, practice and more practice. The only way to get better is reading, completing exercises, challenges and building your own projects/websites.

Landing a tech job is difficult. How long have you been programming for? What other languages have you done?

There are some people who manage to land a job after 6 months, but very few people manage to do so. Many need 1-2 years, if not more.

programming is complicated, and not for everyone.

In the first exercise, requiring us to “fix this method”, I found that you can actually go too far with the fixing.
The code look like this:

console.log(words.some(() => { return word.length < 6; }));

I’m assuming that we are supposed to simply insert word, so that it looks like this:
console.log(words.some((word) => { return word.length < 6; }));

And that works, but if you try to “fix” it a bit more to get this:
console.log(words.some(word => word.length < 6));

it will throw an error: Did you pass word as the parameter to the function?

Am I missing something? Or maybe this other way of writing cannot be recognized as fixing, since the original method is not really broken?

1 Like

The exercise validation isn’t build to handle the shorthand. Given a setup/scaffold is provided in this lesson, only validating the logical choice (adding a parameter) is a lot easier then covering all ways we are allowed to write callback functions.

1 Like

One thing I would like some clarification on is the return process for .filter() vs .every().

const interestingWords = words.filter((word) => word.length > 5);
// output => true

If I put return in the above code, why will I get "unexpected token return"?

console.log(interestingWords.every((word) => { return word.length > 5} ));
// output => true
console.log(interestingWords.every((word) => { word.length > 5} ));
// output => false

Whereas if I don’t put return here, why do I get a "false" value?

Any help is appreciated!!

its a shorthand, when the body of the function is a single line, we can omit curly brackets and return keyword. But this shorthand only works if both (curly brackets and return) are omitted

a return keyword without curly brackets will give an error

curly brackets but no return keyword means you get undefined, which is a falsy value

As I read through the .filter() documentation on MDN I saw this example:

const array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; 

function isPrime(num) {
  for (let i = 2; num > i; i++) {
    if (num % i == 0) {
      return false;
    }
  }
  return num > 1;
}

console.log(array.filter(isPrime)); // [2, 3, 5, 7, 11, 13]

Why is 2 also in the solution and 9 is missing? Looking at the loop, i starts with 2 and in if you see that they test if i % 2 = 0. And 2 % 2 = 0, so shouldn’t it return false then? And 9 should pass as true and be added into the log.

What am I missing here?

2 is prime, 9 is not.

What makes a Prime? When only 1 and that number can be divided into it. 2 is a prime because the only number that can be divided into it is 1, apart from 2. 9, on the other hand is divisible by 3. That makes it not a prime.

For all N greater than 1 this will always be true. Does that make num a prime? Questionable. However, at this juncture in the program it is a bit of a play on words since the yield is very likely, true.

true is the yield we expect when the loop exhausts itself. Nothing would divide into the number so it must be prime. Being greater than zero is moot since the smallest prime is 2 and it is greater than zero, greater than 1, even.

1 Like

I once asked this question, but, after learning that we can assign a reference to a function to another variable, and now, the variable and the function both point to the same function in memory, meaning if you invoke the variable, you are invoking the function stored in the place in memory that it points to, I want to ask again to make sure it relates to what I think it does. When you wrote f(1)(6, 7), you are first invoking f() with an argument of 1. This will cause the f() function to return the subab function (only return it, not invoke it). So now, we got a hold of it, and we can invoke it! This is why we added another set of parentheses. We even passed in two arguments - 6 and 7

Did I get it right?

Pretty much. The f assignment is a function reference, only, not its return value. Now that we have the function which we know takes two arguments (the operands) we can invoke it. We don’t know what the function will be, per se, only that odd numbers get one function, and even numbers get another.

We can find several use cases for this type of logic. Take a simple calculator for instance. It could be fashioned along these lines such that there only ever is one function at the top of pile. What that function is is determined by the input to the factory function (the one assigned to f).