Significance of 'letter' argument?

Tell us what’s happening:
What is the point of the ‘letter’ argument here? I don’t see it being used.

This is one of the solutions provided by fCC, which I am currently going over.

  **Your code so far**

// Adding this solution for the sake of avoiding using 'for' and 'while' loops.
// See the explanation for reference as to why. It's worth the effort.

function fearNotLetter(str) {
var compare = str.charCodeAt(0),
  missing;

str.split("").map(function(letter, index) {
  if (str.charCodeAt(index) == compare) {
    ++compare;
  } else {
    missing = String.fromCharCode(compare);
  }
});

return missing;
}

// test here
fearNotLetter("abce");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36.

Challenge: Missing letters

Link to the challenge:

According to the MDN map documentation, the first argument to the callback function is the element itself, the second argument (not required) is the index of the element in the array. In this solution they are using the index argument but not the element (“letter” in this case) argument. But since the index is the second argument, you have to provide the first argument to get to the second.

1 Like

I don’t understand how the missing letter is found if its increasing both indexes by 1 for the charCodeAt of str (both in variable 'compare 'and ‘str.charCodeAt(index)’ in the function)

Also ‘++compare’ would mean the index used in the variable compare increases by one, correct?

For what it’s worth, this is a big antipattern (ie don’t do this). Map should never be used as a loop like this. Overall, this is not a great piece of code from a readability standpoint.

1 Like

charCode of ‘a’ is 97, ‘b’ is 98, ‘c’ is 99, and so forth. If the first letter is ‘b’ for example, then compare will start from 98. index is the index position of str, so it goes 0, 1, 2, 3, … Every time you move on to the next character, if the letters are in sequence, then charCode of those letters will be equal to compare that is incremented by one at every step.

I also vote this is not a good use of map. When you have to refer to index and the object (str) inside a mapper function, it’s not a good sign.

2 Likes

Oh dear. This is in the guide. We need to replace that solution because it’s very bad code.

This was not written by me. This is one of the solutions provided by fCC FYI.

I updated the solution to something less bad.

FWIW, it isn’t ‘provided by FCC’, The guide solutions are provided by users and this bad one slipped through the cracks during review.


To expand on why this is bad: map is designed to create a new array out of an old array by ‘mapping’ old array values to new array values. A map should never have ‘side effects’, which is to say that it should never change any variables - it should only be used to make a new array.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.