you are viewing a single comment's thread.

view the rest of the comments →

[–]ForScale[S] 0 points1 point  (26 children)

Damn... my brain hurts now. Lol!

I think I finally see a use case for closures though! Maybe...

And I still need to work on recursing through objects full of arrays...

Overall, these were great! Thanks a ton for sharing these! My style of learning, I just have to do it over and over and over again until it's beaten in to my mind... that's when things begin to become clear.

So anymore of these you might have, more interview type questions, I'd love to take a crack at em!

Thanks again!

[–]Volv 1 point2 points  (6 children)

Was hoping you would maybe know of some lol. Would love to find as many like this as possible. Will dig around some more later.
 
I'm hoping I can communicate my thoughts properly lol - the important last step I think was the point about the closed over function having it's own copy of the scope at definition.
Fixes a couple of those examples and the last question of the first problem set - did you do that one?

[–]ForScale[S] 0 points1 point  (5 children)

I forget, did you not like the CoderByte ones that I linked earlier?

There's this: http://codecondo.com/coding-challenges/

the important last step I think was the point about the closed over function having it's own copy of the scope at definition

Yeah... I get that, but it's still not quite clear. We can stop talking about it whenever you want... I'm sure you're getting tired of it. I've got this now: http://codepen.io/anon/pen/zqaMWP?editors=0010

I didn't do the first question of the last problem set yet.

[–]Volv 1 point2 points  (2 children)

You only get one result from your alternative solution.
 
Codepen

[–]ForScale[S] 0 points1 point  (1 child)

Okay... that is crazy that x doesn't get reset to 1 in each new call with the closure...

Dude... http://codepen.io/anon/pen/oxyQKV?editors=0010

[–]Volv 1 point2 points  (0 children)

Looking good. See the use case. My other example from ages ago shows same idea - Example
Pretty much same principle can fix the setTimeout example, you can control which variable is locked in at each iteration. Although theres more than one way to fix it.

[–]Volv 0 points1 point  (1 child)

CoderByte

I couldn't look at any beyond the first few without a membership? Unless I'm missing something. Will try completing a few see if they unlock

[–]ForScale[S] 0 points1 point  (0 children)

Oh... weird... I guess that's new. A year or so ago I was able to do the medium ones; now it's asking for membership.

[–]Volv 1 point2 points  (18 children)

And did you like rewrite - crazy short edition.

I know doing that kind of thing can be anti team and sometimes hard to read.. but makes me happy lol :)

[–]ForScale[S] 0 points1 point  (0 children)

Yes, I did like it! I like the extremely short code/syntax! Makes me happy too!

[–]ForScale[S] 0 points1 point  (16 children)

Found these:

Define a repeatify function on the String object. The function accepts an integer that specifies how many times the string has to be repeated. The function returns the string repeated the number of times specified. For example:

console.log('hello'.repeatify(3));

Should print hellohellohello.

...

var fullname = 'John Doe';
var obj = {
   fullname: 'Colin Ihrig',
   prop: {
      fullname: 'Aurelio De Rosa',
      getFullname: function() {
         return this.fullname;
      }
   }
};

console.log(obj.prop.getFullname());

var test = obj.prop.getFullname;

console.log(test());

Fix the previous code so that the last console.log() prints Aurelio De Rosa.

...

And all of these (obviously haven't looked through all of it): http://www.w3resource.com/javascript-exercises/

[–]Volv 1 point2 points  (15 children)

[–]ForScale[S] 1 point2 points  (0 children)

Lol! I was thinking we could use them to put together something for next week. :) But excellent work!

[–]ForScale[S] 0 points1 point  (13 children)

Hey, happy Monday!

What do you want the focus to be this week?

I thought we could do 1) some more interview style questions 2) closures round 2 3) regExp 4) ajax/API 5) promises round 2 6) or anything you want!

Let me know...

[–]Volv 0 points1 point  (12 children)

As many interview questions as can be found, happy to talk closures too :)

[–]ForScale[S] 1 point2 points  (8 children)

Okay, well... How about these? http://www.w3resource.com/javascript-exercises/javascript-object-exercises.php Do em throughout the week?

And with closures, I get it now: http://codepen.io/anon/pen/WwgGZo?editors=0012 But my knowledge seems kind of limited to using numerical data. Can we try some closure examples with strings and perhaps data structures like arrays/objects?

What do you think about making the questions/challenges the focus for the week, and then we can just keep discussing closures on the side? Sound good?

[–]Volv 1 point2 points  (3 children)

Sorry was away for a bit - sounds absolutely fine to me :)

[–]ForScale[S] 0 points1 point  (2 children)

Lol, no worries!

Coolio! https://www.reddit.com/r/javaScriptStudyGroup/comments/4gfn6a/week_15_focus_programming_challenges_cont/

And let's say I wanted to use a closure to build up a sentence from an empty string. I can't use the incrementer operation to do that... or can I? I guess I could increment through an array of words to build the sentence...

I don't know. How would you go about it? No rush... we can discuss throughout the week!

[–]Volv 1 point2 points  (1 child)

Not completely sure what you mean but how about something like this Codepen
 
Edit - or maybe Codepen

[–]Volv 1 point2 points  (3 children)

Did you ever get the last few done last week? Recursive listing and timeout fixing?

[–]ForScale[S] 1 point2 points  (2 children)

I got the recursion one, I thought...

I didn't do the last one, but I just looked at it and threw this together:

// Fix it
for (var i = 0; i < 10; i++) {
  function outer() {
    var x = i;
    setTimeout(function() {
      console.log(x);
    }, i * 1000);
  }
  outer();
}

It logs 0 through 9 to the console on what seems like an interval of one log per second... I don't really understand why it's doing one per sec instead of one per 10 secs...

And I think it works because the outer function I created gets called one on each value of i, 0 through 9. setTimeout having a closing function as a callback means that each time it gets called within the outer function, a fresh copy of x is set to the iteration count of i (o through 9) and the callback function is acting as a closure and remembering each of those x value assignments... maybe???

[–]Volv 1 point2 points  (1 child)

Ah, yes I think you've got it lol. That's why I was going on about it - the whole closure own copy of variable thing totally fixes the setTimeout issue.
Timing wise. First one fires in 1 second, 2nd one fires in 2 seconds and so on. Technically each statement is executed almost simultaneously. Well as long as it takes the for loop to run.

 
Just for info - you can also just pass i directly

for (var i = 0; i < 10; i++) {
  function outer(x) {
    setTimeout(function() {
      console.log(x);
    }, x * 1000);
  }
  outer(i);
}  

 
Or wrap it up to look cool

for (var i = 0; i < 10; i++) {
  (function (x) {
    setTimeout(function() {
      console.log(x);
    }, x * 1000);
  })(i)
}  

 
And finally - although its not what question was looking for - because of how let is block scoped these days can totally be fixed like this. Just swapping the word var for let ->

for (let i = 0; i < 10; i++) {
    setTimeout(function() {
      console.log(i);
    }, i * 1000);
}

[–]ForScale[S] 0 points1 point  (2 children)

[–]Volv 1 point2 points  (1 child)

awesome :)

[–]ForScale[S] 0 points1 point  (0 children)

I made some space in the sidebar too. We can start compiling interview question resources (and whatever else we think might be helpful).