Why Avoid For loops

Tell us what’s happening:
Hey,
I’m on the Intermediate Algorithm Scripting: Missing letters challenge.

I managed to do it as you see below but after looking at the hints, they said to avoid using For Loops and offered the 2nd solution, from below.

  • Why do we want to avoid For (or while) loops in JS?
  • Why are Higher-Order Functions better suited and recommended?

I imagine that splitting a string into an array and then using .map to loop over each element will take more time and memory than just a simple loop. Please correct me if I’m wrong.

Thanks!
My code


/**
* Find the missing letter in the passed letter 
* range and return it.
* 
* If all letters are present in the range, return 
* undefined.
* 
* fearNotLetter("stvwx") should return "u".
*/

 function fearNotLetter(str) {
   const alph = "abcdefghijklmnopqrstuvwxyz";
   const start = alph.indexOf(str[0]);

   for (let i = start; i < alph.length; i++) {
     if (!str.includes(alph[i])) {
       return alph[i];
     }
   };
   return;
}

/** OR  */
/** TO AVOID FOR LOOP */

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;
}

// const a = fearNotLetter("abce");
const b = fearNotLetter("stvwx")

console.log(b)

Challenge: Missing letters

it’s personal choice, higher order functions tend to be more readable, but loops are usually more performing

2 Likes

Maybe just so you expand your breath of knowledge a bit? I tend to default to for loops for almost every programming task, and had to be dragged, kicking and screaming, into 2015.

And I only started this year!

Seriously, though, map() is pretty awesome. forEach() can get things done quickly. for in is good, but for of is great.

2 Likes

Hey @organic_darius!

I wrapped your solution in spoiler tags [spoiler] for those in the forum who haven’t worked on this challenge yet.

I am also a beginner, and I too default to for loops as my first choice. When I go back and refactor my old solutions I try to use a different method so I can grow with javascript.

2 Likes

One is imperative the other declarative (imperative vs declarative).

Personally for me, declarative wins every time. Not to be presumptuous, but I think with time and after having written more declarative code you might agree as well.

2 Likes

We don’t, often it’s easier to use a loop. But for doing operations on arrays you have the array methods which are going to cover 90% of your usecases without needing to explicitly write a loop. Those methods also make off-by-one errors impossible, and because of the way they’re designed, can be chained together.

Sure, but the time difference is often a matter of microseconds. And yes, if you split and map and join you’re doing more work than looping over a string and building a new one. Note this difference is tiny in this case, not noticeable. But in other cases unless it’s really important that the whole operation is optimised, the simpler (and easier to debug) way is going to be splitting it into a series of simpler operations, even though that may make the code less efficient. Optimised code will be more specialised and more fragile.

2 Likes

Hi,

Thanks, everybody for taking the time to write insightful replies. :pray:
It makes more sense now and I’ll keep that in mind for future challenges.
Love this community! :orange_heart:

1 Like