Code Efficiency

Tell us what’s happening:
So, my question is that using for loop is efficient compared to built in functions like map(), reduce()?

Your code so far


function findLongestWordLength(str) {

let maxCount = 0;
let counter = 0;

for(let i = 0; i < str.length; i++) {
  if(str[i] != ' ' && str[i] != '
'){
    console.log(counter);
    counter++;
  }
  else {
  counter = 0;
  }
  if(maxCount < counter){
      maxCount = counter;
  }
}
let call = findLongestWordLength("What if we try a super-long word such as otorhinolaryngology");
console.log(call);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15.

Challenge: Find the Longest Word in a String

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/find-the-longest-word-in-a-string

methods like map and reduce are doing loops under the covers.

You might want to look into time complexity and Big O notation. Like @ArielLeslie said, map and reduce are doing loops without you seeing them.
A basic for loop as a time complexity of O(n) because you need to touch every element at least once. So it’s linear time.
Since the map is like a for loop, the time complexity is O(n) as well. They’re both equivalent at the base.

It gets super complicated, but you should be able to jump off of this and begin your journey into discovering time complexity!