Two Plus Two Publishing LLC
Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > >

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 09-12-2018, 08:03 PM   #35176
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,497
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

I'm talking about with console.log(someObject) - different but similar problem.

What are the requirements of #1? Does it have to be a for loop? Where can I change the code provided?

Last edited by suzzer99; 09-12-2018 at 08:09 PM.
suzzer99 is offline   Reply With Quote
Old 09-12-2018, 08:08 PM   #35177
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,497
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Code:
/** Console.log out 0-4 inside the timeout */
for (i=0;i<5;i++) {
  let j = i;
  window.setTimeout(function() {
    console.log(j)
  }, j * 100)
}
Btw this seems to work. But not if you do var j instead of let j. Good illustration of var vs. let.

Actually this is all you need to do:

Code:
/** Console.log out 0-4 inside the timeout */
for (let i=0;i<5;i++) {
  window.setTimeout(function() {
    console.log(i)
  }, i * 100)
}
i is being set as a global variable when you don't put let in front of it.
suzzer99 is offline   Reply With Quote
Old 09-12-2018, 08:09 PM   #35178
Craggoo
culled
 
Craggoo's Avatar
 
Join Date: Sep 2006
Location: R.I.P. ItzPenzoo 12-09-11
Posts: 12,533
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by suzzer99 View Post
I'm talking about with objects - different but similar problem.

What are the requirements of #1? Does it have to be a for loop? Where can I change the code provided?
There is no real issue with the traverse problem so forget I sort of said there was.

This is what you must output in the setTimeout problem. The console.log *must* be inside the timeout. That is the only thing that makes the problem tricky.

Craggoo is offline   Reply With Quote
Old 09-12-2018, 08:11 PM   #35179
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,497
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

See my answers above.
suzzer99 is offline   Reply With Quote
Old 09-12-2018, 08:13 PM   #35180
Craggoo
culled
 
Craggoo's Avatar
 
Join Date: Sep 2006
Location: R.I.P. ItzPenzoo 12-09-11
Posts: 12,533
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

He never brought up any ES6 topics. With that in mind, I doubt he would allowed me to use let. I'm now kinda intrigued why it works with let but not with var.

Lets say you aren't allowed to use let, do you know how you would accomplish it?
Craggoo is offline   Reply With Quote
Old 09-12-2018, 08:15 PM   #35181
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,497
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

This illustrates the issue I was talking about. You get the final state of the object every time, even though the timeout is set to 0.

Code:
/** Console.log out 0-4 inside the timeout */
const myObj = {};
for(let i=0;i<5;i++) {
  myObj.index = i;
  window.setTimeout(function() {
    console.log(myObj)
  }, 0)
}
I was wrong about console.log itself being async, it's when there's anything async happening in the flow.
suzzer99 is offline   Reply With Quote
Old 09-12-2018, 08:18 PM   #35182
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,497
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by Craggoo View Post
He never brought up any ES6 topics. With that in mind, I doubt he would allowed me to use let. I'm now kinda intrigued why it works with let but not with var.

Lets say you aren't allowed to use let, do you know how you would accomplish it?
Let forces i to be block-scoped, to the for loop.

I'll play around with the var option.
suzzer99 is offline   Reply With Quote
Old 09-12-2018, 08:20 PM   #35183
Craggoo
culled
 
Craggoo's Avatar
 
Join Date: Sep 2006
Location: R.I.P. ItzPenzoo 12-09-11
Posts: 12,533
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by suzzer99 View Post
Let forces i to be block-scoped, to the for loop.

I'll play around with the var option.
Its really simple questions like these that illustrate sometimes just how little (or much) you know. You cannot adjust the staggered timeout either. So setting the value to zero is a no-no.
Craggoo is offline   Reply With Quote
Old 09-12-2018, 08:24 PM   #35184
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,497
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

W/o let you have to wrap the setTimeout in a function like this. It works because var is function-scoped but not block-scoped.

Code:
/** Console.log out 0-4 inside the timeout */
for(i=0;i<5;i++) {
  (function () {
    var j = i;
    window.setTimeout(function() {
      console.log(j)
    }, i * 100)
  }());
}
Or just pass i as a function param like this.

Code:
for(i=0;i<5;i++) {
  (function (index) {
    window.setTimeout(function() {
      console.log(index)
    }, index * 100)
  }(i));
}
Note the function doesn't have to be self-executing. It just seemed cleaner that way.

Yeah I probably get hung up on these in interview settings when my brain freezes up. I might get through them but my speed wouldn't wow anyone.
suzzer99 is offline   Reply With Quote
Old 09-12-2018, 08:26 PM   #35185
goofyballer
Carpal \'Tunnel
 
goofyballer's Avatar
 
Join Date: Jun 2005
Posts: 66,681
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by suzzer99 View Post
Good illustration of var vs. let.
Heh, I almost put let instead of var in my answer but I think I was swayed by the rest of the code around it being old-school style. Drats!

Quote:
Originally Posted by Craggoo View Post
There is no real issue with the traverse problem so forget I sort of said there was.

This is what you must output in the setTimeout problem. The console.log *must* be inside the timeout. That is the only thing that makes the problem tricky.
Quote:
Originally Posted by Craggoo View Post
Lets say you aren't allowed to use let, do you know how you would accomplish it?
Function binding? I forget the exact syntax, but something like

Code:
for (i=0;i<5;i++) {
  window.setTimeout((function(j) {
    console.log(j)
  }).bind(null, i), i * 100)
}
(null first because iirc the first arg to bind sets the value of `this`)
(that may well have the same problem though and idk what the hot codepen sites are to actually try it)
goofyballer is offline   Reply With Quote
Old 09-12-2018, 08:32 PM   #35186
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,497
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Heh, apparently this works too.

Code:
/** Console.log out 0-4 inside the timeout */
for(i=0;i<5;i++) {
  window.setTimeout(function(index) {
    console.log(index)
  }, i * 100, i)
}
https://stackoverflow.com/questions/...meout-callback

Quote:
In modern browsers, the "setTimeout" receives a third parameter that is sent as parameter to the internal function at the end of the timer.

Example:

var hello = "Hello World";
setTimeout(alert, 1000, hello);
Crazy - I never knew that. Works in node too.
suzzer99 is offline   Reply With Quote
Old 09-12-2018, 08:34 PM   #35187
Craggoo
culled
 
Craggoo's Avatar
 
Join Date: Sep 2006
Location: R.I.P. ItzPenzoo 12-09-11
Posts: 12,533
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by goofyballer View Post

Function binding? I forget the exact syntax, but something like

Code:
for (i=0;i<5;i++) {
  window.setTimeout((function(j) {
    console.log(j)
  }).bind(null, i), i * 100)
}
(null first because iirc the first arg to bind sets the value of `this`)
(that may well have the same problem though and idk what the hot codepen sites are to actually try it)
You have it almost right. I think you had an extra opening parentheses and bind was in the wrong place. The first argument is the `this` value which, in this scenario, we could care less about. We can pass any amount of arguments through when we use bind. By passing it the current value of i, we capture the value as it was (not as it will be).

Code:
for (i=0;i<5;i++) {
  window.setTimeout(function(j) {
    console.log(j)
  }.bind(null, i), i * 100)
}
Edit: yours works too but is generally not how I would use .bind. I would always stick it at the immediate end of a function definition. There's no need to wrap the function in () for .bind to work.

Last edited by Craggoo; 09-12-2018 at 08:42 PM. Reason: whoops
Craggoo is offline   Reply With Quote
Old 09-12-2018, 08:37 PM   #35188
goofyballer
Carpal \'Tunnel
 
goofyballer's Avatar
 
Join Date: Jun 2005
Posts: 66,681
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

goofyballer is offline   Reply With Quote
Old 09-12-2018, 08:38 PM   #35189
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,497
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

.bind is easily the highest cognitive debt way to solve this, so interviewers will probably love it.
suzzer99 is offline   Reply With Quote
Old 09-12-2018, 08:45 PM   #35190
goofyballer
Carpal \'Tunnel
 
goofyballer's Avatar
 
Join Date: Jun 2005
Posts: 66,681
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

I think C#, oddly enough, works the same way - I've encountered that problem before. I suppose that's one of the reasons C++ is nice with its explicit capture. The same code wouldn't have that problem:

Code:
for (int i = 0; i < 5; i++) {
  lambda = [&] {
    std::cout << i << std::endl;
  };
  // ...do something with lambda
}
The "&" specifies that all variables used inside the lambda (in this case, i) should be captured by reference. Alternatively you could replace that with "=" to make them captured by value.

I suspected, but only now confirmed, that Go works like JS/C#: https://play.golang.org/p/cCKgPpZuMY7
goofyballer is offline   Reply With Quote
Old 09-12-2018, 08:56 PM   #35191
Craggoo
culled
 
Craggoo's Avatar
 
Join Date: Sep 2006
Location: R.I.P. ItzPenzoo 12-09-11
Posts: 12,533
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by suzzer99 View Post
.bind is easily the highest cognitive debt way to solve this, so interviewers will probably love it.
Googling that term brought up a bunch of medical articles instead of an actual definition. I suspect I know what cognitive debt means but you could restate it in more obvious terms?

I can tell you that if you plan on working in React then you better be familiar with call/bind/apply because you'll likely be using them a lot.

I haven't worked in React but I do use .bind quite a lot.
Craggoo is offline   Reply With Quote
Old 09-12-2018, 08:59 PM   #35192
goofyballer
Carpal \'Tunnel
 
goofyballer's Avatar
 
Join Date: Jun 2005
Posts: 66,681
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

idk, I like bind a lot more from a "look at this code and understand what it's doing" (which I think you're talking about, suzzer?) perspective than the "yo dawg, I heard you like functions so I put a function in your function" solutions.
goofyballer is offline   Reply With Quote
Old 09-12-2018, 09:11 PM   #35193
suzzer99
Save the Cheerleader, Save the World
 
suzzer99's Avatar
 
Join Date: Nov 2005
Location: on top of the bell curve
Posts: 91,497
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Right but this is probably the simplest.

Code:
/** Console.log out 0-4 inside the timeout */
for(i=0;i<5;i++) {
  window.setTimeout(function(index) {
    console.log(index)
  }, i * 100, i)
}
Doesn't work in IE8 though. Does work in IE10 and up. Not sure about IE9.
suzzer99 is offline   Reply With Quote
Old 09-12-2018, 09:17 PM   #35194
maxtower
Carpal \'Tunnel
 
maxtower's Avatar
 
Join Date: Sep 2005
Posts: 7,891
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by goofyballer View Post
I don't know if my company is particularly inaccurate, but my current salary at my current employer is well over what h1bdata/glassdoor show for engineering salaries.

For Netflix, as an example (they seem like a good barometer since they forgo stock for more base salary), Glassdoor says "senior software engineer" averages out to $206k. I'm not leaving my job and moving to the South Bay for that, but I also feel like that's probably significantly undercutting what a senior software engineer actually makes there (remember the $325k figure earlier ITT? I'd probably pack up for that much!).

Maybe part of the problem is that much like how college degrees became ubiquitous, now errybody is a "senior software engineer" and the term is meaningless.
Yeah, these aren't going to be very accurate, but you will hopefully be able to use it to filter some posers. Like if you want to make $200k, you know you don't have to bother with regular programming jobs at companies with a range from 80-120 or whatever. It's just unlikely that they are going to meet your expectations. (Oh they'll still want to know all about your whiteboard skills though)
The smaller a company is, the fewer data points they have, so that will introduce more inaccuracies. These are self reported, and smaller places will be paying their best people to not leave, so those salaries won't be in the mix. Also aren't salaries appreciating at like 10%/yr in the bay area ? If so any salary reported 2 years ago is 20% low.
To get the big salaries a company's product has to either be very valuable (like B2B industrial software) or has to have real scale like millions of users. They have to think they are competing with the FANGs for talent and be funded enough to do so.
Everybody else is trying to skirt by picking up scraps.
maxtower is offline   Reply With Quote
Old 09-12-2018, 09:33 PM   #35195
Craggoo
culled
 
Craggoo's Avatar
 
Join Date: Sep 2006
Location: R.I.P. ItzPenzoo 12-09-11
Posts: 12,533
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by suzzer99 View Post
Right but this is probably the simplest.

Code:
/** Console.log out 0-4 inside the timeout */
for(i=0;i<5;i++) {
  window.setTimeout(function(index) {
    console.log(index)
  }, i * 100, i)
}
Doesn't work in IE8 though. Does work in IE10 and up. Not sure about IE9.
My googling says that it only works in IE10+. That is why the .bind approach is better. The only requirement for it to work is .bind is present (which I expect it would be on every browser including the most ancient ones).
Craggoo is offline   Reply With Quote
Old 09-12-2018, 09:50 PM   #35196
microbet
Solar Powered
 
microbet's Avatar
 
Join Date: Jan 2005
Location: SoCal
Posts: 53,143
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

This is probably dumb, but I didn't know bind or about the scope of let and was trying to figure a different way. Dunno if it had to be in a for loop. But, this also seems to work:
Code:
/** Console.log out 0-4 inside the timeout */
i = 0;
window.setTimeout(function() {
        while (i<5) {
                window.setTimeout(console.log(i), i * 100);
                i++;
        }
}, i * 100);
microbet is offline   Reply With Quote
Old 09-12-2018, 10:35 PM   #35197
Craggoo
culled
 
Craggoo's Avatar
 
Join Date: Sep 2006
Location: R.I.P. ItzPenzoo 12-09-11
Posts: 12,533
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

Quote:
Originally Posted by microbet View Post
This is probably dumb, but I didn't know bind or about the scope of let and was trying to figure a different way. Dunno if it had to be in a for loop. But, this also seems to work:
Code:
/** Console.log out 0-4 inside the timeout */
i = 0;
window.setTimeout(function() {
        while (i<5) {
                window.setTimeout(console.log(i), i * 100);
                i++;
        }
}, i * 100);
I'm really surprised that
Code:
window.setTimeout
doesn't complain when you pass it anything other than a function as the first argument.
Craggoo is offline   Reply With Quote
Old 09-13-2018, 01:07 AM   #35198
microbet
Solar Powered
 
microbet's Avatar
 
Join Date: Jan 2005
Location: SoCal
Posts: 53,143
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

I was listening to a podcast and they were talking about promises. I did this on my phone. It did

0
1
2
3

I think the 4 should come but the thing I was using cut off the output at 4 lines.

Code:
for (i=0;i<5;i++) {
    var promise1 = new Promise(function(resolve, reject) {
        setTimeout(resolve(i), i*100);
        promise1.then((successMessage) => {
          console.log(successMessage);
        });     
     });
  }
microbet is offline   Reply With Quote
Old 09-13-2018, 03:19 AM   #35199
microbet
Solar Powered
 
microbet's Avatar
 
Join Date: Jan 2005
Location: SoCal
Posts: 53,143
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

I got home and tried it on my computer and it didn't get the 4. You have to set i<6 to get the 4. It's always 1 less. If you say i<60, it goes to 58. The last time through the for loop it doesn't output.
microbet is offline   Reply With Quote
Old 09-13-2018, 05:05 AM   #35200
Larry Legend
Celtic Pride
 
Larry Legend's Avatar
 
Join Date: Jul 2009
Location: Kyrie's earth
Posts: 42,693
Re: ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

I've seen and used this interview question a lot so if I was being cheeky and the question was to do it as "fast as I could" I would do:

Quote:
/** Console.log out 0-4 inside the timeout */
for (let i=0;i<5;i++) {
window.setTimeout(function() {
console.log(i)
}, i * 100)
}
But then in modern browsers you could do:

Quote:
var myFunc = function(i) {
console.log(i)
}

for (i=0;i<5;i++) {
window.setTimeout(myFunc(i), i * 100)
}
In node you would do:

Quote:
var myFunc = function(int) {
return function() {
console.log(int)
}
}

for (i=0;i<5;i++) {
setTimeout(myFunc(i), i * 100)
}
There are fancier ways to do it but I feel like those would be enough.

Quote:
/** Given an element and a callback, traverse an element and its children (and children's children [recursion]) calling the callback on each element with the element itself as the sole argument. */
function traverse(element, callback) {}
This is very similar to an interview question I had for my current job. Traverse an object of unknown complexity and length and then the interviewer added a few changing requirements. Ternary because why not?

Quote:
var traverseElements = function(element, callback) {
element.children ? traverseElements(element.children, callback) : callback(element)
}
Larry Legend is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Forum Jump


All times are GMT -4. The time now is 07:22 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright 2008-2017, Two Plus Two Interactive
 
 
Poker Players - Streaming Live Online