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);
}
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);
}
Share
Improve this question
edited Dec 9, 2012 at 18:29
halfer
20.4k19 gold badges109 silver badges202 bronze badges
asked Dec 9, 2012 at 17:52
Yamaha32088Yamaha32088
4,19310 gold badges53 silver badges105 bronze badges
2
- 1 Do you need to find your whole name intact or just the individual letters in it? Will the result have 3 elements or over 30? – ErikE Commented Dec 9, 2012 at 18:29
- just the letters I solved it though with the help of @crowjonah' suggestion – Yamaha32088 Commented Dec 9, 2012 at 18:37
4 Answers
Reset to default 2The 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.
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/wCWxr/1/). The problems with your original code include:
you try to pare
text[i]
tomyName[i]
but the indices of the two strings won't match up.you try to push the entire string
text
intohits
instead of one character at a timeyour logic doesn't deal with the possibility that the beginning but not the end of
myName
is intext
, 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.
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!");
}
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.