Intermediate Algorithm Scripting - Drop it

Hello everyone, i need to solve a challenge, the challenge is to:

If the word in the length is bigger or equal to five, then i should return the string and the word should be reveresed.

I called the function below, and passed some arguments to see if things worked out.

The words that’s length is greater than or equal to five are:

“fellow, warriors” >>>> Same string.
“another”.

I wrote the code below, and wrote a conditional statement to test the length of the word. And while the above words length is bigger than or equal to five, i get

“nothing”

Which was supposed to be returned if the if statement was false.
Now i need to know.
1- why when i did the else statement got executed when i have words that their length is bigger than or equal to five?

const spinWords = string => {
  let array = string.split(" ");
  for (let word of array) {
    if (word.length >= 5) {
  let change = word.split("").reverse().join("");
  return array.join(" ").replace(word, change);
    } else {
      return array.join(“ “);
    }
  }
};

console.log(spinWords('This is a test')); // 'This is a test'
console.log(spinWords('Hey fellow warriors')); // 'Hey wollef sroirraw'
console.log(spinWords('This is another test')); // 'This is rehtona test'

Your browser information:

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

Challenge Information:

Intermediate Algorithm Scripting - Drop it

Put the `console.log()’ into your code to troubleshoot some more. Like this:

const spinWords = string => {
  let array = string.split(" ");
  for (let word of array) {

    console.log(word,word.length)

    if (word.length >= 5) {
      let change = word.split("").reverse().join("");
      console.log(change)
      return array.join(" ").replace(word, change);
    } else {
      return array.join(" ");
    }
  }
};

You will see more clearly what is not happening.

The loop stopped on the first words of the the strings. It didn’t continue to include the rest words.

When i use:

console.log(word, word.length);

the output i get is limited to only the first word of a string

That’s correct, your loop only looks at the first word of the string, and then stops.

console.log(word, word.length); does not affect your program, it will print every word it loops through. Look at your if statement. If the first word length is >=5 it executes the split reverse join.

If the first word is not >=5 it returns array.join immediately.

It never looks at the 2nd word. That’s what the console log is telling us.

Why it doesn’t look for the second word? And how to write a for loop correctly which will loop through all elements and apply the if statement code to all the elements.

Follow the logic of your if statement. If the word is less than 5, it will Return. Return ends your function. The first word is less than 5 so it Returns the string and ends the function immediately.

You want to move return array.join(" "); outside of the loop, so that it only executes once the loop is finished.

You can paste your code here to follow the execution: https://pythontutor.com/python-compiler.html#mode=edit

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