.push isn't working


#1


it looks like my .push isn't working?


/*jshint multistr:true */
var text = "You can ignore Lara the jshint... line for now. All that\ does is tell the Lara console to stop worrying about Lara our use of\ backslash characters for wrapping long lines of Lara text.";

var myName = "Lara";

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


#2

Is it not working or are you just not using it at all?
How can you find out which is the case?

What is the result of adding a number to a string, does that make sense?
Is the first letter of myName "L"? Or is it myName[0]? If you change the name, should the first letter still be "L"? Or should the whole program be rewritten when the data changes? If the solution is hard-coded into the program then it'll only have a single result and there wouldn't be much point in running the program, it would be easier to just save the result in a text file.


#3

as of right now with

var text = "You can ignore Lara the jshint... line for now. All that\ does is tell the Lara console to stop worrying about Lara our use of\ backslash characters for wrapping long lines of Lara text.";

var myName = "Lara";

var hits = [];

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

it shows me a number instead of letters


#4

Shows? Keep in mind that you do not have any console.log calls in your code, so I don't think you should expect any particular value to be shown.

That's quite a bit of progress though. I suggest fixing the indentation as well. And I still suggest finding out whether or not push is "not working" or if you're just not executing it - I'd add a print where it's being called. If that print doesn't print anything out then that part of the code isn't being executed


#5

yes it works now, thank you.

/*jshint multistr:true */
var text = "You can ignore Lara the jshint... line for now. All that\ does is tell the Lara console to stop worrying about Lara our use of\ backslash characters for wrapping long lines of Lara text.";

var myName = "Lara";

var hits = [];

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

console.log(hits)

the console now shows all the right letters but in an array with a string of every letter of my name.
do you know how I could make this became a string of my name.


#6

You've pushed characters into an array, so that's to be expected.
What you'd need to do to make that a string is to concatenate them all together.
In some languages strings are immutable and concatenating an array of characters needs to be done all in one go otherwise it'll be terribly inefficient. In others you can just add them all one at a time to an empty string.
In javascript they are immutable, so it should be done in one go, you need some built-in functionality which does that all at once for you:


#7

And the code should be formatted more like so:

var myName = "Lara";
var hits = [];

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

#8

I don't know what to do here?? :frowning:


#9

That < has a really wide angle, is it a < ? maybe that's just your font.
If that's not the issue then start comparing your syntax carefully with what's shown at https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for and you can also comment out what you have and start with just a basic for-loop, getting that to work before adding the rest back in.

Generally just narrow down the part that's producing the error message - comment out and create a minimal example that's feasible to compare to documentation/examples


#10

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