2016年4月21日 星期四

What would you do except waiting for luck?

I have tried a very annoying coding challenge yesterday. It's about guessing the random number of the output of Math.floor(Math.random() * 100 + 1). What!? Guessing the number? Even the possibility of doing that is only 0.01. That means you should get it to right in at most 100 trials. However, you know, we are engineers! So, we need a way of engineers to solve this.


Here is the link of the problem and the screenshot of it. ClickMeToTheProblem





Here are some of my trials: (in chronological order)

1. Make the guess to be the var of lucky_number. As expected, it throws out an undefined error because the checking is after executing my code.

2. Guess some numbers.

3. Make the guess to be a function so that it can be get the value of lucky_number later using something like eval('lucky_number').

4. Guess some numbers again ...... :)

5. Make the guess to be a lazy evaluation variable or function so that it can be in the scope of the expected number when it is evaluated.

However, none of these are working because I messed up with the scope. After a few trials, I feel like this problem is unsolvable. Then I tried to look at some discussions. I saw a person asking why the method of changing the kernel random method is not working. It did inspire me. Now, I know the answer ......


Just right before the declaration of my guess, put this magic code:

Math.floor = function (a) {
    return 1
}
var myGuess = Math.floor('hacked it :)')

This question is really interesting and funny to play around. I have spent about 2 hours on this simple question. But I feel like I am leveling up. @@






P.S. I thinking to `open source` this blog plugin which is built in react.js and make it handy for other people to plug in their sites. Let's see.





沒有留言:

張貼留言