Search for your text name shortening the code


#1

hi guys i just wanted to put the complete string of my name in the hits[] array and want to shorten the code

/*jshint multistr:true */
var text = "This is Zori some text Zori containing random names Zori \ or shiz like that Zori";
var myName = "Zori";
var hits = [];
var pushName = "";
var count = 0;

for (var x = 0; x < text.length; x++){
    if(text[x] === myName[0]){
        
        for(var y = x; y < myName.length + x; y++){ 
            if(pushName == myName){  
                pushName = "";
                pushName += text[y];
            }else{
                pushName += text[y];
            } 
            if(pushName == myName){
                hits.push(pushName); 
            }
        }        
    }      
}
if(hits.length === 0){
    console.log("Your name isn't found");
}
console.log(hits);
console.log(pushName);

it seems kinda redundant inside using 2 if statements but if i put the
hits.push(pushName);
inside the first, the result will omit atleast one name string


#2

Please always format your code by selecting it and pressing Ctrl+Shift+C :smile:

Here is your upgraded code - https://repl.it/BcLz -

/*jshint multistr:true */
// combine multiple var declarations
var text = "This is Zori some text Zori containing random names wrong Wori but Zori or shiz like that Zort but Zori",
    myName = "Zori",
    hits = [],
    pushName = "",
    count = 0, x, y;

for (x = 0; x < text.length; x++){
    if(text[x] === myName[0]){
        pushName = ""; // begin empty
        
        for(y = x; y < myName.length + x; y++){ 
            pushName += text[y]; // keep adding new characters
            
            // whenever it equals the name, 
            // just push it in hits array exit the inner loop
            if(pushName == myName){
                hits.push([pushName, x]);
                break;
            }
        }
    }      
}

if(hits.length === 0)
    console.log("Your name isn't found");

console.log(hits);
console.log(pushName);

With the following, cleaner and better, changes:

  1. Combined var declarations
  2. Removed redundancy of ifs and fixed code logic
  3. Now you can also see the index in the string where your name appears.

Hopefully, it will serve as a learning experience for you :smile: If there's any troubling piece of code, please reply to this thread without hesitation :smile:

P.S. This code can be improved severely further, but this is the best I could manage without deviating much from the original exercise :wink:

Hope it helps!


#3

Thanks for the reply and sorry about that just new here.
it seems silly now that i have to think for hours just to make a way to turn it into a string XD
BTW what does that break exactly do? it just says that it stops a block of code and which one does it stop? the if statement or the for lopp?


#4

I am sorry, but I think I went ahead in the course material a little - you might not have been exposed to the break statement yet - but you'll be soon. Don't worry :wink:

So, I have rewritten the code to remove break statement - https://repl.it/BcLz/1. You'll like it better hopefully :smile:

/*jshint multistr:true */
// combine multiple var declarations
var text = "This is Zori some text Zori containing random names wrong Wori but Zori or shiz like that Zort but Zori",
    myName = "Zori",
    hits = [],
    pushName = "",
    count = 0, x, y;

for (x = 0; x < text.length; x++){
    if(text[x] === myName[0]){
        pushName = ""; // begin empty
        
        // construct name
        for(y = x; y < myName.length + x; y++)
            pushName += text[y]; // keep adding new characters            

        // if constructed name equals original name, 
        // just push it in hits array exit the inner loop
        if(pushName == myName)
            hits.push([pushName, x]);             
    }      
}

if(hits.length === 0)
    console.log("Your name isn't found");

console.log(hits);
console.log(pushName);