Open Side Menu Go to the Top
Register
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

09-12-2018 , 08:08 PM
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.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:09 PM
Quote:
Originally Posted by suzzer99
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.

** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:11 PM
See my answers above.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:13 PM
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?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:15 PM
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.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:18 PM
Quote:
Originally Posted by Craggoo
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.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:20 PM
Quote:
Originally Posted by suzzer99
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.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:24 PM
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.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:26 PM
Quote:
Originally Posted by suzzer99
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
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
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)
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:32 PM
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.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:34 PM
Quote:
Originally Posted by goofyballer

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
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:37 PM
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:38 PM
.bind is easily the highest cognitive debt way to solve this, so interviewers will probably love it.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:45 PM
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
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:56 PM
Quote:
Originally Posted by suzzer99
.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.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 08:59 PM
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.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 09:11 PM
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.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 09:17 PM
Quote:
Originally Posted by goofyballer
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.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 09:33 PM
Quote:
Originally Posted by suzzer99
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).
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 09:50 PM
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);
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-12-2018 , 10:35 PM
Quote:
Originally Posted by microbet
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.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 01:07 AM
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);
        });     
     });
  }
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 03:19 AM
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.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 05:05 AM
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)
}
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
09-13-2018 , 05:30 AM
Quote:
Originally Posted by microbet
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);
        });     
     });
  }
If you move your promise1.then outside of the definition of the promise, then you get 0-4:

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);
    });
}
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote

      
m