Question 7 Attempts With Separate Array But Returns Error


#1


Hello Everyone!

Let me brief you with the background of the problem:
Question 7 was something that greatly intrigued me, so I tackled the problem head on, resulting in the code below. Compared to the code that was purposefully made, I added an extra empty array to push in the separate characters of myName. This array would be made outside of the comparison chunk of the code to make the variable the array was held in global scope and able to reach the whole mess of coding. The array was only made to compare the values of text and my name to make sure that "Eddie" would not come up.

The problem? Well, the console reads:
TypeError: name.push is not a function
Well, of course it isn't a function! I did the same inside of the comparison chunk and that error did not seem to mess with the debugger. I thought I did the correct syntax and concept.

What help do I need? It would be grateful if many other intelligent coders helped me out with this problem. There are two things that I question:

1.Why did the debugger give this error message?
2.Am I heading the right path to correctly differentiate multiple names with my own? What do I still need to work on to make the code run properly?

/*jshint multistr:true */
var text = "HiHiHiEricHiHiHiHEriciHiHiHiEricHiHiHi\
HiHEriciHiHiHiHiEricHiHiHiHiHiHiHEriciHiHiHiHiHiHE\
riciHiHiHEriciHiHiHiHiEricHiHiHiHEriciHiHi";
var myName = "Eric";
var hits = [];
var name = [];

for(var o = 0; o < 5; o++)
{
    name.push(myName[o]);
}

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

if (hits.length === 0)
{
    console.log("Your name wasn't found!");
}
else
{
    console.log(hits);
}

Anyways, It would be wonderful if people could respond with their opinions. I apologize if this question is too stupid or repetitive. I just want to know the answer for my own attempts.


#2

@brokenavocado

  1. This for loop shouldn't be there according to the instructions. I'm not really sure why there is this error too. Try refreshing the webpage.

  2. I think you're going in the right path. Keep it up!


#3

Really? I tried refreshing the page many times, it does nothing but redirect me back to question 6 but i'm still able to access question 7. This is weird.

Thank you very much!


#4

Hi Eric,

I'm still working on this one myself (though I might call it a night for now. I've got the program to recognize my name the first time but not in future instances...), but I've noticed a few things about your push error.

  • I think your for loop may execute one too many times (if indexing starts at 0, the for loop runs for five letters-worth)
  • If you want to have an array of your name before you start comparisons, why not just set var myName [ E, r, i, c]? (Not a criticism, just curious. Also, looking at your program I don't yet see a reason for this as myName and name (once corrected) have the same length.)
  • Finally, regarding the error, I tried putting in random console.logs to confirm which push is the issue (it's the first one).

To the solution: changing your Eric array from name to heyname resolves the issue:

for (var i = 0; i < 4; i++) {
    heyname.push(myName[i])
}

console.log(heyname)

prints [E, r, i, c]


I'm not sure why (perhaps someone more experienced can weigh in), but when I googled "name a function javascript", one of the results is the following excerpt from Mozilla's dev page:

The function.name property returns the name of the function

If I am understanding this correctly it would seem that name is a built-in function; therefore you can't assign variables and such the name name.


Finally, reading some of the hints on other posts, I've gathered that the key (based on what we've been taught to this point) is to make a second temporary array that holds letters; i.e. once your program finds E it stores that and the 3 subsequent letters into a temporary array and adds them to the hits array if they match myName.

The way I've been going about this is to only add letters to my temp array if they match their corresponding letters in myName. Another way to do it that just occurred to me now is to push the matching number of letters after E to the temp array regardless of what they are and then compare the temp array to myName; I haven't tried this yet so I'll have to test it later before I personally can say whether it's possible (maybe someone else can chime in).


What I did was to nest my text for loop in my myName for loop and create a temp array as described above. However as I mentioned this only detects the first instance of myName and simply switching the order of the nesting just makes the program detect nothing, so I'll need to do a bit more messing around on this later or post it here if I can't figure it out. (Update: I tried it.)

Anyway, you might've moved on by now, but in case you were still curious, I hope I helped shed some light on the situation! :slight_smile:

--frisby


#5

Hi @miniapple8888,

I think the problem may be that there is already a built-in function assigned "name", so we can't assign it to our own things. Otherwise you are right; the syntax seems to be spot-on.

--frisby