An unwelcome 'undefined' result - why?!


#1

Hey there!
So, when I run my code I find results that are encouraging but still.. not good.
And it's not that I want to fix it, I'm going NUTS trying to find my mistake.
Can someone help me? Thanks!

RESULTS:
[ 'M', 'a', 'r', 'g', 'h', 'e', 'r', 'i', 't', 'a', undefined ]

CODE:

var text = "Io mi chiamo Margherita";

var myName = "Margherita";

var hits = [];

for (var i=0; i<text.length; i++) {
    if (text[i] === myName[0]) {
        for (var j = i; j <=(myName.length + i); j++) {
            hits.push(text[j]);
            }
        }
    
    }
    console.log(hits);

#2

I think your problem is literally just

right here you shouldn't have the equal sign, it should just be the less than, <, sign. :slight_smile:


#3

ohhh, thanks! I'm literally both relieved and kind of disappointed :smiley:


#4

Your code is fantastic, and the hardest errors to solve are the one's you barely notice. When that happens, just go through line by line or write pseudo code. I'm glad you got it to work. :slight_smile:


#5

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