5/7 It is so difficult for me to understand!


#1

So this is my code:

var text = "eok eok oek oke Rem oek oek oke oke Rem oke Rem";
var myName = "Rem";
var hits = []

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

I read some topics about this and @leonhard.wettengmx.n helped someone out with this aswell. I understand it a bit better now and when I chop my code and go thru all the parts and saying to myself (I know it is weird) what this part of code this than I can sometimes understand it. And than when i have something wrong and I look it up and see the answer than I can see why I am wrong. But sometimes it's just so weird were I'm like &%*# I don't get it why. Like for example:
for(var j = i; j < i + myName.length; j++) {
hits.push(text[j]);
};

I see all those variables and those + and other stuff and it is confusing me so much and than I just get lost in the code. Maybe you guys will not understand me but if you do can you please help me to find a way to not get lost into the code. Maybe this is such a weird question but I think it's normal.

-Rem


#2

Well don't worry Rmino
There are some careless mistakes needed to be just cleared.
First of all try to make your code a little spacious

remove the var from here we don't need that

2.

You need to put semicolon at the end

3.

here see we don't need the "var" in the loops. Remove it
4.

same here
maybe the code works with that but i dont think its a good practice.

5.

i would never put semicolon in this way
maybe it works but try to avoid it go in a certain order
LIKE I DID

ALL IS OK NOW
maybe if i miss something i apologize

HERE is the correct code

/*jshint multistr:true */

text = "eok eok oek oke Rem oek oek oke oke Rem oke Rem";
var myName = "Rem";
var hits = [];

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

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

#3

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