Checking past the first letter of your name


Absolute beginner here, in the end my main piece of code looks like this:

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

It does what it's supposed to and lets me pass the test, but I want to improve the code by also having it check the rest of the letters of the myName variable. I first thought of this alteration of the first if-statement:

if (text[i] === myName[0] && text[i+1] === myName[1] && text[i+2] === myName[2] && text[i+3] === myName[3] && text[i+4] === myName[4] etc... ) {

I dismissed it because it looks messy and I don't like solving it this way. I want to do it using another loop, or maybe even a function. But how do I go about this? I'm only asking for a push in the right direction, I really want to write the code myself.


Before going into the second FOR-loop
you could define a variable collectText
var collectText = "";

Then in your second FOR-loop
you could make use of JavaScript's string concatenation facility
by concatenating every text[ j ] onto collectText
collectText += text[ j ]

Then after leaving the second FOR-loop
you could use a =strict= comparison, to compare collectText with myName

if ( collectText === myName ) {
      //IF code-block


google search
== the Book ==
javascript [your question]

== discussions / opinions ==
javascript [your question]

== guidance ==


teach yourself javascript
Adding Event Handlers The Simple Way
JavaScript and Cookies
Dealing with the DOM
JavaScript and CSS

Yourself Python:


Thanks a lot! It took me a while, but I figured it out!