Reputation: 4163
I am learning JavaScript through Codecademy, but I have an issue. The code below is supposed to search through the text
variable for my name in the myName
variable and then push all of the individual letters to the hits
array. The code that I have written is not correct but Codecademy says that it is correct and is going to let me move on in the lesson.
I have been trying to solve the issue that I am having with no luck. The problem is that when I run the hits.push(text);
line it will output the entire variable but I have tried hits.push(text[i]);
and get undefined for the result. Can someone please help me understand where I have made the mistake?
/*jshint multistr:true */
var text = "XsddfasASSFABrandonSFsdfdasBrandonsddfadfaBrandon";
var myName = "Brandon";
var hits = [];
for (i=0; i<=text.length;i++){
if (text[i]===myName[i]){
for(var x=i; x<i+myName.length;x++){
hits.push(text);
}
}
}
if (hits.length===0){
console.log("Your name wasn't found!");
} else {
console.log(hits);
}
Upvotes: 0
Views: 6172
Reputation: 1
if (text[i]===myName[i]){
this line should create an error, because myName[i] is not the first letter of myName.
if (text[i]===myName[0]){
Change to this line should work.
Upvotes: 0
Reputation: 9868
Normally you would do this kind of thing using indexOf
, match
, search
, substr
or substring
, which are all string methods.
However for the purpose of this exercise you can do:
var text = "XsddfasASSFABrandonSFsdfdasBrandonsddfadfaBrandon";
var myName = "Brandon";
var hits = [],
namePosition = 0;
for (var i = 0; i < text.length; i++) {
if (text[i] === myName[namePosition]) {
hits.push(text[i]);
namePosition ++;
if (hits.length === myName.length) {
break;
}
}
else {
namePosition = 0;
hits = [];
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}
(See it working at http://jsfiddle.net/wCWxr/1/). The problems with your original code include:
you try to compare text[i]
to myName[i]
but the indices of the two strings won't match up.
you try to push the entire string text
into hits
instead of one character at a time
your logic doesn't deal with the possibility that the beginning but not the end of myName
is in text
, e.g. if text was aerwerBrasdfsgars
My suggestion fixes this by recording (with namePosition
) what position we are currently at within the string myName
, and incrementing that when we find a character in text
that matches the relevant character in myName
. If the characters do not match then it's not a true hit, so we reset hits = []
and namePosition = 0
. If the characters all match then hits
eventually reaches the length of myName
and so we break out of the loop.
Upvotes: 2
Reputation: 2878
The best way I can think to explain your mistake is simply by walking through a bit of the logic of what you have written.
for (i=0; i<=text.length;i++){
Your for
loop will iterate i
for as many characters as there are in your text
variable, so: 49 times.
if (text[i]===myName[i]){
The first run through your for
loop, where i=0
, you are checking to see if text[0]
is strictly equal to myName[0]
. text[0] = X
and myName[0] = B
. The strictly equals condition is not met, so the loop proceeds to increment i
repeat: text[1] = s
and myName[1] = r
. This continues 47 more times, and the condition is never met. myName[i]
is undefined after the first 7 loops.
Upvotes: 2
Reputation: 43850
If you are trying to find if myName is in text here is what you do:
RegExp:
var pattern = new RegExp(myName);
if (pattern.test(text)){
console.log(myName);
}else {
console.log("Your name wasn't found!");
}
indexOf:
if (text.indexOf(myName) != -1){
console.log(myName);
}else {
console.log("Your name wasn't found!");
}
Upvotes: 0