Freigeben über


Micro Blog Post - Open Door / Closed Door Problem and TypeScript Solution

 

The open door / closed door problem was just something I was messing with after an interview question about it.

Wanting to verify that my original solution was actually correct (it was)

 

You can check it out here on typescript playground (which is a sweet playground):

https://www.typescriptlang.org/Playground/#src=%0Aclass%20Door%20%0A%7B%0A%20%20%20%20status%3A%20boolean%3B%09%0A%7D%0A%0Avar%20doors%20%3D%20new%20Array()%3B%0A%0Afor%20(var%20i%3D0%3B%20i%3C100%3B%20i%2B%2B)%0A%7B%0A%09var%20newDoor%20%3D%20new%20Door()%3B%0A%09newDoor.status%20%3D%20false%3B%0A%09doors.push(newDoor)%3B%09%0A%7D%0A%0Afor%20(var%20x%3D0%3B%20x%20%3C%20doors.length%3B%20x%2B%2B)%0A%7B%0A%09for%20(var%20y%3Dx%3B%20y%20%3C%20doors.length%3B%20y%20%3D%20y%20%2B%20x%20%2B%201)%0A%09%7B%0A%09%09if%20(doors%5By%5D.status%20%3D%3D%20false)%0A%09%09%7B%0A%09%09%09doors%5By%5D.status%20%3D%20true%3B%0A%09%09%7D%20%09%0A%09%09else%0A%09%09%7B%0A%09%09%09doors%5By%5D.status%20%3D%20false%3B%09%0A%09%09%7D%0A%09%7D%09%0A%7D%0A%0Afor%20(var%20z%20%3D%200%3B%20z%20%3C%20doors.length%3B%20z%2B%2B)%0A%7B%0A%09var%20listthing%20%3D%20document.createElement('li')%3B%0A%09listthing.textContent%20%3D%20doors%5Bz%5D.status%3B%0A%09document.body.appendChild(listthing)%3B%0A%7D%0A%0A

 

 

Open Door / Closed Door problem - given n number of doors:

1) Go through the list of doors, and "toggle" the state of every door

2) Go through the list of doors again, starting with the "next" door, toggle every other door

3) Go through the list of doors again, starting with the "next next" door, toggle every other other door 

4) And on and on