Victory! (Name Search+) Array Organization Question


#1

Hi everyone,

After twenty or so iterations and the helpful hint from these forums to utilize at least two arrays, I finally got my program to work, and with multiple iterations of myName in the text (hence the extra yo mama to make sure)! Hooray! It could probably be more eloquent (any feedback on this point is of course welcome), and I know the instructions suggested googling built-in methods, but I was determined to figure out how to complete the bonus assignment with the techniques we've learned so far.

However, that brings me to my next point...

Has anyone thought of a short, neat way to put a space between instances of myName in their hits array such that it is more legible for names with spaces, using only techniques we've learned in the course?

i.e. rather than the console.log of my array looking like y,o, m,a,m,a,y,o, ,m,a,m,a, it looks like y,o, m,a,m,a, y,o, m,a,m,a.

I've tried adding a space at the end of each push, but that results in a comma after the space rather than before. (I don't think this is something arrays are meant to do, but anyway I wanted to try.)
(Although if I log it as an array it does look neat since I opted to keep each temp array as a nested array rather than add another for loop to avoid this).

I suppose another way to handle it would be to push a space before each nested array, but then there would be an unnecessary space at the beginning that would have to be handled (I'm thinking with another for loop).

Any thoughts or feedback is welcome, and if you've approached the bonus assignment differently I'd love to hear about that too, etc. :slight_smile:

Thanks everybody!
frisby

https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/7?curriculum_id=506324b3a7dffd00020bf661


/*jshint multistr:true */

var text = "yo gamma gamma\ yo mama\ fi fi fo fama\ mama\ yo mama!";
var myName = "yo mama";
var hits = [];
var tempHits = [];

for (var i = 0; i < text.length; i++) {
    if (text[i] === myName[0]) {
        for (var k = 0; k < myName.length; k++) {
            if (text[i+k] === myName[k]){
                tempHits.push(text[i+k]);
                if (tempHits.length === myName.length) {
                    hits.push(tempHits);
                    tempHits = [];
                }
            }
            else {
                tempHits = [];
            }
        }
    }
}

console.log(hits + ", Mister Falcon!!");


Question 7 Attempts With Separate Array But Returns Error
Fixing the promiscuity of hits with endsWith()
#2

Well you could use a for loop to go through your hits array to build a string from the entries and add a space every myName.length entry e.g.

Spoiler Alert

var str = ""
for( var i=0;i<hits.length;i++){
    str+=hits[i];
    if( i%myName.length == 0){
       str+=" ";
    }
}


#3

Whoa, cool! I forgot you can add to strings like that!!

While I think technically that hasn't been explicitly taught yet in this course, in retrospect I should've thought to experiment with it when the instructions stated we could call on specific characters of a string as with an array... :scream:

Thank you for continuing to broaden my mind, @haxor789. Your guidance is really helpful! :smile:


#4

I did some searching on the webs and it is way easier than i thought.

This script prints my name whenever it is mentioned and the number of times it was found.

Here is the code:

// text that im searching my name in
var text = " Hey, how are you \
doing? My name is Snib27 blah \
blah blah blah Snib27 blah \
Snib27 blah blah .";

/* variable that does a global "/g"
   search for the  string "Snib27"
   using "match()" */
var nameFinder = text.match(/Snib27/g);

// if my name isn't in the text
if (nameFinder === null) {
    console.log ("Your name wasn't found.");
}
/* prints my name, and the number of
   times it is mentioned in the text string */
else {
    console.log (nameFinder);
    console.log ("\nYour name was found " + nameFinder.length + " times.");
}

#5

Thank you for sharing, @snib27! If I may ask, what does "global search" mean? Does adding /g make it catch all instances of your name as opposed to just the first one?


#6

Exactly!

We can use regular expressions also in the replace method, here is example of global and not global replace:

var text = "yo gamma gamma\ yo mama\ fi fi fo fama\ mama\ yo mama!";
console.log("Not global replace: " + text.replace(/gamma/, "epsilon"));
// >> Not global replace: yo epsilon gamma yo mama fi fi fo fama mama yo mama!
console.log("Global replace: " + text.replace(/gamma/g, "epsilon"));
// >> Global replace: yo epsilon epsilon yo mama fi fi fo fama mama yo mama!

You can also use i flag to make your search or replace case insensitive.


#7

Aha I see, although I have to say, "epsilon" isn't quite as rhymey-wimey :grin: Thank you for explaining, @factoradic! For some reason the concept of regular expressions hasn't been very sticky for me, but hopefully I'll get there eventually.