My code works, but gets rejected, for "JavaScript: Search Text for Your Name"


#1



https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661


My code behaves correctly, and outputs exactly the same output as the beginning "example" code did, and yet I get:

Oops, try again.
Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.

And will not let me progress. Even though my second 'for' loop clearly does stop when it reaches its current point in the string + myName.length.

I'm suspecting that it is because I didn't write the code exactly the same way they did, but that is silly, it should let me solve it in an alternative way.

Note: I added the console.log(hits); after I was getting the error, to try and troubleshoot, only to find it behaves perfectly well. So that isn't what is confusing it. Even without it, it gets rejected.

The output I got was [ 'S', 't', 'e', 'e', 'n', 'S', 't', 'o', 'n', 'e', 'S', 't', 'e', 'e', 'n' ]

Note, I capitalized 'Stone' to highlight to myself that this solution to finding a name in a bunch of text doesn't work well. Obviously, if I didn't capitalize it, it wouldn't return it as my name. I've also tried writing it as 'stone' and then I get [ 'S', 't', 'e', 'e', 'n', 'S', 't', 'e', 'e', 'n' ] which is the intended output, but still the code is rejected.


/*jshint multistr:true */

var text = "Oh Steen, oh bubbles and babboons and stones, \
Forsooth Steen, but why do the sturdy gears grind all alone?";

var myName = "Steen";

var hits = [];

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

console.log(hits);


#2

refresh the browser and try the same code! :slight_smile:


#3

Alas, it is still rejected with the same error message.

I guess I'll just rewrite it to exactly match the way they want it. I'm just stubborn, so it feels like a defeat lol.


#4

for (var i=n; i< myName.length+n; i++)


#5

I know that's what they want, but my code does literally the exact same thing, so I figure it should be accepted. They should be welcoming of the idea that there are many ways to solve the same problem, and promote that thinking.

But, sure enough, when I changed it to for (var i=n; i<(myName.length+n); i++) I got the exact same output, and everything behaved identically, and yet I was allowed to advance.

Like I said, I'm just bull-headed is the main problem. I guess I should be grateful for what they've made and ignore the hiccups and bugs.


#6

I've changed it back to (i-n) in the next part of the exercise, let's see if it still freaks out about it, or if it is water under the bridge between the exercise and me at this point.


#7

yes! you are right :slight_smile:
you cant do that in the lesson but good news is there is another place you can do that :slight_smile:
labs repl.it


#8

Fair enough! Thanks :slight_smile:


#9

I had the same issue with yet another way code the same result:

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

I got around it by doing it like they wanted j < i + myName.length (which is actually wrong in this case), and then changing it back when I got the green button :wink:


#10

Haha, I also changed mine back after I got the green button, and it magically accepted it from there on in.