Console.log name 5 times


#1

/*jshint multistr:true */
var text = "Hey, how are you chris. i'm good, how are you? i'm / great.";
var myName = "chris";
var hits = [];
for (var i=0; i < text.length; i++) {
if (text[i] === "c") {
for (var j = i; j < i+myName.length; j++){
hits.push("chris");}
}
}
if (hits.length === 0) {
console.log("You're name wasn't found");
}
else {
console.log(hits);
}

that's my code, but it prints my name "chris" 5 times: [ 'chris', 'chris', 'chris', 'chris', 'chris' ]

please, what is wrong with it


#2
if (text[i] === "c") {
    for (var j = i; j < i+myName.length; j++){
        hits.push("chris");
    }
}
  1. The loop is executed if the ith letter of the text (start counting with 0) is "c". There's just one such letter whose index number is 17.
  2. j is 17 (equal to i), and i+myName.length is 17+5=22. There will be 5 iterations of the loop, for i equal to 17, 18, 19, 20 and 21.
  3. hits.push("chris"); - the thing this code is doing is adding "chris" to the "hits" array. That's exactly what will be executed 5 times, so that's why you get an array with five "chris" elements!

But your goal is to "push" letters which computer finds into the array, not your name. So, here's the right line:

hits.push(text[j]);

I used it as an example when was explaining you the "push" method :wink:


#3

saved my life twice in a day, Goddamn. :smile:
Thanks a million. but now i'm curious, at Victory 7/7, I was told
"Now, as we mentioned, this system isn't perfect. For instance, if the paragraph contains both "Eric" and "Eddie", we'll see this in our hits array:
['E','r','i','c','E','d','d','i','e'];

Think about how you might fine-tune this program to make sure it only finds exact matches for your name. Search the Internet to see if there are any built-in JavaScript string methods that can help!
"

do you know the method to fine tune such a program to search for the name in specific, I've been racking my brain but all I can think of is a way to collapse the array into a single element and then create an if/else function to determine if the substring(1,4) of the hits will be "hris". Do you know of any such command or is there another way of doing it.
once again, thanks a million


#4

Here's one of ways you can do it:

/*jshint multistr:true */
var text = "Hey, how are you chris. i'm good, how are you? i'm / great.";
var myName = "chris";
var hits = [];
for (var i=0; i < text.length; i++) {
    var found = "";
    if (text[i] === "c") {
        for (var j = i; j < i+myName.length; j++){
            found += text[j];
        }
    }
    if (found === myName) {
        hits.push(myName);
    }
}
if (hits.length === 0) {
    console.log("Your name wasn't found");
}
else {
   console.log("Your name was found " + hits.length + ((hits.length === 1) ? " time." : " times."));
}

#5

Even better:

/*jshint multistr:true */

var text = "Hey, how are you chris. i'm good, how are you? i'm / great.";
var myName = "chris";
var hits = [];

for (var i = 0; i <= (text.length - myName.length); i++) {
    if (text.substring(i, i + myName.length) === myName) {
        hits.push(myName);
    }
}

if (hits.length === 0) {
    console.log("Your name wasn't found!");
} else {
    console.log("Your name was found " + hits.length + ((hits.length === 1) ? " time." : " times."));
}

#6

WOW, followed well up to console.log("you're name wasn't found"); but thoroughly ruffled after that, would you mind explaining the last line?


#7

maxtirdatov, I found the syntax for the search, it's pretty simple actually.
First, define your string as a var:

var stringName = "this is the original string"

Then simply use a .search syntax on your stingName like so:
var searchResult = stringName.search(" ")
then input the specific word you are searching for in the quotes within the brackets.

after which you can print and the result printed will be a number showing the specific position of the word searched for. keeping in mind the computer counts from 0 ofcourse.

so say we defined a var as var str = "this is our string"
and apply the .search as var n = str.search("is")
and then we print the n var console.log(n)
our result will be 5 because the positon of the first character of our search (i) is on the 5th position
Hope this helps, I tested it myself and it seems legit


#8

OK) It's just a shorter way to do this:

if (hits.length === 1) {
    var times = " time.";
} else {
    var times = " times.";
}

console.log("Your name was found " + hits.length + times);

// console.log("Your name was found " + hits.length + ((hits.length === 1) ? " time." : " times."));
// Syntax: (condition) ? value1:value2
// used to return one of two values depending on the condition right away
// value1 is returned if the condition is true, otherwise value2

#9

ball of wisdom, thanks a million for you're help today. hope to hear from you in future more.