Wednesday, January 22, 2014

Stupid Cursor/ While Loops

     I'm back at the coffee shop! I spent some time being diverted by Google Now on ChromeOS, but I'm at it now. I have my latté, I have 2 Cellos and Escala playing (I'm in a cello mood today) So, now I'm going to plug in and plug away!

     Today, I'm working on a program that is aiming to find a certain word in a wall of text. It seems similar to Python with <page.find> sort of thing. Not sure what this comment:</*jshint multistr:true */> means yet, but I'm sure I'll figure it out soon enough. The backslash seems like a nicely useful addition to my knowledge center. It lets you break a line without it being considered a statement of any sort. It lets the text wrap around as it were. Found this out about the weird comment:"You can ignore the /*jshint... line for now. All that does is tell the console to stop worrying about our use of backslash characters for wrapping long lines of text." Seems Like something that will be used a lot in later lessons.  This program is using <for> loops and arrays to start the lesson. I like where this is going. <if/else> statements that include an array are proving themselves to be a foe to my memory bank. I keep forgetting how to do it, but I'll get it down soon enough. Never fear, reader person! Memory will win! At some point! And now they mention the <push> feature before they've even told me about it. Time to Google away! So, I think I figured it out. 
     Now, this is just crazy... they had me do this:
/*jshint multistr:true */

var text = "blah blah blah blah blah\
blah Martin blah blah blah blah Martin\
blah blah blah blah blah Martin blah blah\
blah blah blah";

var myName = "Martin";

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


But then someone posts something that means this:
/*jshint multistr:true */

var text = "blah blah blah blah blah\
blah Martin blah blah blah blah Martin\
blah blah blah blah blah Martin blah blah\
blah blah blah";

var myName = "Martin";

var myName = text.match(/Martin/gi);
console.log(myName);

     Does the same exact thing?? What?? I feel like they're taxing the long way around so that I can learn the most stuff. At least, I hope that's what they're going for.
     
     "Get in the habit of typing exactly as much as you need to, and no more!" I just came across that in the lesson. That is precisely what I'm going for! Clean, simple, and minimalistic code is what I'm going for. Am I incorrect in thinking what I'm striving to attain is a good thing? While it is subjective, as is anything, I feel like I am not incorrect. 

     For some reason my cursor is being quite hinky about moving when I tell it to while writing my code, but it's fine on here. That's no bueno. I hope it doesn't get too bad.

     Thank goodness for the forums! The people on there explain things much better than the instructor does almost every time! Aaaaaand I'm looping. Pretty sure I'm doing exactly what they said not to do. Oh well. Now to fix it. The first problem was that I forgot to put <var> before my variable. Wow. I made a variable and forgot to set it as a variable? What a rookie mistake!! The second thing was that I had <variable === false> for some reason it needed <variable=false> was the correct way to do it, though I'm not sure why that changed things. It seems that when you have <===> you're saying it's equal to something, but when you have <=> it means it is something. So, now that I know that, I'll know not to do what I did. 

     This is me:
var plip= 0;
var thoughts= "I have thoughts...";
var ploop= function(thought1,thought2){
    while(plip<30){
        plip++;
        console.log(thoughts + thought1 + thought2);
    }
};
ploop("Why,"," stupid cursor??");

Yup. At least 30 times! That damn cursor just didn't want to work. Ah well. Here's to hoping it works better next time... So, I learned about <while> , I learned that I'm going to need better earphones; these aren't staying in the port on my computer every time, which is no good when you're in a public place. I also learned that cursors can be really stupid. And, in the end, I learned that I'm probably going to love <while> loops. See y'all Friday! Today is Wednesday, right? Yes. Yes it is. Ok. Back to hectic life! Cya!
--Martin