Exercise "log it" help


#1

alright, so it says i've passed though it seems to not be working properly; the output is spam of "your name was not found" even though my name is inside
/*jshint multistr:true */

var text = "blah blah Dan blah blah blah \
Dan blah blah blah blah Dan blah blah blah \
blah blah blah blah blah Dan\
blah blah Dan blah blah blah \
Dan blah blah blah blah Dan blah blah blah \
blah blah blah blah blah Dan";
var myName = "Dan";
var hits = [];

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


#2

Is it your opinion that your code includes all the instructions required to find the name?

Consider what needs to happen, without looking at the code, just as a mental idea.
Convince yourself that those steps are sufficient, no point in writing it down otherwise!

Then match that up to your code.


#3

i mean, i've redone it several times and i'm getting /some/ results, though they're not exactly the results i want. i guess i'll just keep trying then?

still, thanks! i guess i'm just not thinking enough when doing the exercises, the visualization before i type should really help thanks a bunch!


#4

Right, so you could for example write down the comments for those steps, and then fill in the code.

So if you have one such comment per logical step, then you can debug those steps individually by checking the state of the program before and after the step has been executed.

If the comments/logical step add up, and the code in each step matches its comments, then everything should work!

The intention is to never keep too much in your head at once, so that reasoning about what you're doing becomes easy.

Functions help a lot with this. Stow away the details in a function and name the function after what logical action it represents. And with those functions we can then create increasingly powerful functions - this is how we build complicated applications out of extremely simple machine instructions.


#5

comments would definitely be great, but most of the time i'm stuck on figuring out what to actually write inside of them aha

still, great advice! i'll try using comments more often since now that i think about it looking at my code it's probably a real pain to decipher as the semicolons pretty much blend together and it's hard to see what i'm doing; comments would do wonders in that aspect!

also if anyone is wondering, as embarrassing as it is i just ended up not following the instructions properly. after i created another if condition outside of my initial loop it worked like a charm! the push statement was also poorly written as it was supposed to be hits.push(text[j]) so yeah the code was a total mess!

thanks ionatan for not spoon-feeding me, it probably wouldn't have been as satisfying solving it had you given me the answer instead of giving me advice ; v ;b


#6

That's where google comes in! Use it liberally. There's no need to try to memorize what you needed to look up, it'll stick if you have to look it up enough times or otherwise you don't need to remember it anyway.

So in summary.. Split up the task into smaller units. Split those units up as well if non-trivial.
Convince yourself of that those units can be done, assume that you'll do it correctly. Convince yourself that the units when used together correctly, will produce the correct result. You can even write code that depends on stuff that doesn't exist yet, that is perfectly reasonable. And whenever you don't know how to do one of those units, google.

Pretty sure we're equipped to solve world hunger now.

Instructions - yeah - we humans tend to fill in the blanks and think that when we have an understanding, that we have the understanding. I suppose this is where we need to get a bit bran damaged and think in a more literal manner.

And yeah, no, just supplying the answer is a poor time investment for both of us. Much better to take a step back and work on how to approach the problem.


#7

i will take all of this to heart, or at least do my best to do that!

now onwards towards pulling my hairs out because of objects! :^)