0

I'm trying to find the even length strings of a function that will take an array of values, and I should then return an array of all the even length strings.

Anything that's not a string, or not a string of even length, should not be returned in the array.

If there are no even length strings, it should return an empty array.


I've managed to return an empty array when passed an empty array, keep even length strings, remove odd length strings and removes non-strings. However, when given multiple strings that are even it only gives the first one. For example, for the function findEvenLengthStrings(["dogs", "cats"]) it only gives "dogs" but doesn't give cats.

Been working on this for a while and can't find simular questions. I also only understand for loops at this point so an array reduce may confuse me.

Here is my code so far!

function findEvenLengthStrings(items) {
    let empty = []
    let evenLength = []
    for (let i = 0; i < items.length; i++) {
        if (items[i].length % 2 === 0) {
            evenLength.push(items[i])
        }
        return evenLength
    }
    return empty
}
isherwood
  • 58,414
  • 16
  • 114
  • 157
  • 2
    The `return` statement inside the loop will exit the function; that's what `return` does. If you don't want that, then don't include that statement. – Pointy Aug 05 '22 at 12:41
  • 1
    Instead of returning from inside the loop, return *after* the loop. I don't see a need for the `empty` array at all. Just return `evenLength`, which may or may not have elements. – David Aug 05 '22 at 12:45
  • @Pointy Thanks for you reply. The task that I'm trying to complete requires my to return an array with only even length strings. I'm not sure how to do this without using the return tool (which stops the loop) and if I try and put it out of the function it says its outside of the function so won't work that way either. – Syd Plonce Aug 05 '22 at 12:54
  • But that's why `cats` is not appearing - because you're terminating the loop before it gets to that element. Just `return evenLength` _after_ the loop has completed. [Some example code](https://jsfiddle.net/cmr9u4vL/2/). – Andy Aug 05 '22 at 13:02
  • @Andy Thanks for your help. I understand that return stops a loop function. However, I need to return the array somewhere and i'm getting errors when I try to return it elsewhere. If I replace where the "return empty" is now with "return evenLength" (Which looks to me to be outside of the loop) it still comes up with issues if I try an array like ["hiya", 1, "nice", "bye", null] as arguments, it says "TypeError: Cannot read properties of null (reading 'length')". . – Syd Plonce Aug 05 '22 at 13:04
  • 1
    You don't need the `empty` array. If you don't push anything into `evenNumbers` and then return it after the loop has finished you'll return an empty array. Look at my examples - I'm also checking `typeof items[i] === 'string'`. – Andy Aug 05 '22 at 13:05
  • 1
    @andy Thanks Andy, after combining the typeof with putting the return outside of the loop it is working in all cases now. Thanks very much for your help I understand it now, I was just very consufsed as to why moving the return to outside of the loop was causing more errors but the typeof line has solved that and made it sleek! Legend – Syd Plonce Aug 05 '22 at 13:11

0 Answers0