Log it!


#1

Getting an error saying "Did you remember to log the result of your search to the console?". I'm probably just being thick and missing something basic out, can anyone help?

var text = "chickens lemons turles Callum, dogs, yellow, pretty cows, likely rainy when tuesday... sleeps, Callum continue tomorrow"
var myName = "Callum"
var hits = []

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

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


#2

Hi,

This is your corrected code:

var text = "chickens lemons turles Callum, dogs, yellow, pretty cows, likely rainy when tuesday... sleeps, Callum continue tomorrow";
var myName = "Callum";
var hits = [];

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

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

Issues:

  • Look at yours 1, 2 and 3 line: Don't forget about ";"
  • Look at your 5 line for (var i = 0; i <= text.length; i++): here must be < but not <=. Remove = here!
  • Look at your 7 line for (var j = 0; j < (i + myName.length); j++). Change here var j = 0 to var j = i!
  • Look at your 8 line hits.push(text[i + j]). Change it to hits.push(text[j])
  • Look at your 13 line in your code if (hit.length === 0), here is a mistake! Change hit.length to hits.length.

Now your code will run as expected! :slight_smile:


#3

But why does the 'j' suddenly show up here?


#4

Hi! I passed this lesson, but it isn't showing up right. Here's my code:

/*jshint multistr:true */

var text = "Hello , Bob , Nice to meet you, Bob , how are you, Bob ?";
myName = "Bob"
var hits = [];
for(var i = 0; i < text.length; i+=1){
if (text[i] === 'B'){

for(var j = i; j < (myName.length + i); j++){
hits.push(text[i + j]);
}
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!")
} else {
console.log(hits)
}

Help!


#5

see hits.push(text[i + j]); //here it's wrong

you should push hits.push(text[j]);

#6

I passed this lesson also, but it isnt shows to me right too. Could you please, help me?
Below is my code

var text = "Hello, how are you doing? My name is Antonina";

var myName = "Antonina";

var hits = [];

for (var i = 0; i < text.length; i++) 
{
if (text[i] === myName[0]) 
{
for (var j = i; j < myName.length+i; j++) {
hits.push(myName[j]);
}
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}

#7


if (text[i] === myName[0]) // do you really want it?

so do

if (text[i] === 'A')

and

what is the value of myName[j] //

you should do text[j]

hope it'll make sense :slight_smile:


#8

It showed up like it was supposed to. Thanks! :smile:


#9

you are right, but I wasn't online since now. So , I am going to thank him. :slight_smile: :slight_smile:


#10

Thank you for your help ! Now it works , looks better. :slight_smile: :tada:


#11

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