
Eric Stanley- 13 Sep, 2019
Game Show Host Problem
The game show host problem which is also called as ‘Monty Hall Problem’ is one of the classic math puzzle, which best explains the odds of probability. A game show host and a player (you/me) are the ones who are involved in this puzzle. The puzzle has 3 doors out of which 2 doors will have nothing in it and 1 door will have the treasure, which the user is intended to find. The game show host first asks the player to pick a random door. Let’s say the player picks the door number 1. Now, the game show host opens door number 3 and reveals that the 3rd door has nothing in it. After which, the host asks the player whether he/she likes to change the answer. Let’s get into the math now, before answering the host’s question. Considering the initial odds that the user had which is 33.33% chance of hitting the correct door, which has the treasure. Now, how I got this 33.33%? Alright, let’s say there is only 1 door, which has the treasure, and the host asks to pick the door that has the treasure. Now, that there is only 1 door, there is no choice for you/me to pick the wrong door. Which gives the probability of finding the correct door is 100% (1 out of 1 door). In case of 2 doors, the probability reduces to 50% (1 out of 2 doors) and 33.33% in case of 3 doors. That’s how I got the 33.33% Now that we know the initial odds, let’s check the new odds after the host asks the player if he/she wishes to change the door. There are only 2 possible answers to this question. You can either change the door or stick with the door that you/me chose initially. Let’s check the probabilities for either of these answers, so that we can understand how the odds change based on the choice that we make. It’s easy to assume that the odds to finding the right door after the question is 50%. Coz’, after the opening the 3rd door, we only have 2 doors to open out of which 1 door has the treasure, which makes the probability to 50%. However, what we are ignoring here is the initial odds (probability). Let’s see how the initial probability changes the odds of the outcome of the host’s question. Let’s first assume that we are not changing the door and we are sticking with the door # 1. In which case, the chances of winning the treasure is still 33.33%. Which means, whether or not the host asks the question, we are not using it effectively to increase our chances to win. The probabilities before and after the question still stays the same and that means we had 33.33% chance to win the prize and we have the same percentage of probability now. Now, what happens if we change the door? We know that door # 3 has nothing, so if we plan to change door now, we have nothing else but door # 2. Considering the initial odds (33.33%), let’s calculate the odds of winning the treasure now. Though, the door with nothing in it (door # 3) was opened, we now have a choice to open 2 doors instead of 1 door if we change our answer. Door # 3 is already open, and we are opening door # 2. Otherwise, we are opening only 1 door (door # 1) irrespective of the door already opened. Meaning, the host has already given us an extra 33.33% by opening the door with nothing in it and if we change the door now to door # 2, we have a 66.67% chance of hitting the right door (opening 2 out of 3 doors). Remember, no host wants the player to win. So, the host is always going to open the door with nothing in it, coz’ there are 2 doors which has nothing in it, which means irrespective of whether you pick the correct treasure door or a wrong door, the host will always have the option to pick the door which has nothing in it. It’s was quite hard to explain this problem in plain text than I thought, but if you get this right. Watch out for the odds next time!

Eric Stanley- 21 Aug, 2019
Ternary Operators
The ternary operator is basically another way to implement the if-else statement. Now, if it’s just another way to implement the if-else statement, why can’t I use the same old if-else? Why would I need to take up something extra which I don’t need right now? As for any new change, the answer to the above question is simple. Kinda hard, but it’s worth it. It’s not absolutely necessary to learn it, as you can do anything and everything without it. However, considering the constant evolution of technology, it’s better to learn new things while we still can! After all, learning new things is always interesting. Let me put this straight. This post is not to blame on the good old if-else statement. If you are not clear on the if-else statement, I would recommend you to google it before you proceed further. Before we get into what a ternary operator is, let’s get a quick bite on what an operator and operand is, and different kinds of operators in general Operator: These are the symbols that we use in our calculations. Ex: + - / = * % Operand: These are the variables or constants the are being used with the operator for calculations There are 3 different types of operatorsOperator DescriptionUnary Operators Requires one operand. Ex: a++Binary Operators Requires two operands. Ex: a+bTernary Operators Ternary Operators: Requires three operands Ex: c ? a : bLet’s start with a simple example as usual. A typical if statement looks like below if (latitude > 0) { return 'summer' } else { return 'winter' }Let’s put the above statement in a function, so that we can see the actual working result function getSeason(latitude) { if (latitude > 0) { return 'summer' } else { return 'winter' } }document.write(getSeason(10))If you want to see how the above code works, I would recommend codepen.io, however if you already know how this works; or, you already are part of another online code editor community, good for you 🙂Moving on, the getSeason function takes one argument which is the latitude and returns a season string based on the latitude. Ternary operators are all about reducing the number of lines of code. So, before implementing the ternary operator, let’s see if we can reduce the number of lines without it. If the if-else statement has only one line of code to be executed, then the {} is actually not necessary. The above code can be written as function getSeason(latitude) { if (latitude > 0) return 'summer' else return 'winter' }document.write(getSeason(10))Not much of code reduction here, but the point is, you can avoid the {} in case of one statement within the if-else block Note: Anything more than one statement within the if-else would require the {} Time to implement ternary operator in our above example. function getSeason(latitude) { return latitude > 0 ? 'summer' : 'winter' }document.write(getSeason(10))Looks like the code is reduced a lot now, but how about nested if statements. Let’s take a look at a little more complex example by adding another variable called month to derive the season, like below function getSeason(latitude, month) { if (latitude > 0) { if (month >= 1 && month <=6) { return 'summer' } else { return 'winter' } } else { if (month >= 1 && month <=6) { return 'winter' } else { return 'summer' } } }document.write(getSeason(10, 5))As you can see, the code is a lot more complicated and the number of lines has significantly increased this time. Good news, is the ternary operator can be nested too. The above example can also be written as function getSeason(latitude, month) { return latitude > 0 ? (month >= 1 && month <=6) ? 'summer' : 'winter' : (month >= 1 && month <=6) ? 'winter' : 'summer' }document.write(getSeason(10, 5))Now, all the code in the nested if-else example can be written just in one single line. On a side note, we can get the current month and current latitude programmatically. The final code now looks like below function getSeason(latitude, month) { return latitude > 0 ? (month >= 1 && month <=6) ? 'summer' : 'winter' : (month >= 1 && month <=6) ? 'winter' : 'summer' }new Promise((res, rej) => navigator.geolocation.getCurrentPosition(res, rej)).then(pos => document.write(getSeason(pos.coords.latitude, (new Date().getMonth()+1))))Make sure to click on Allow when you get the prompt for requesting access to location. Now what is the new statement that I have written to extract the current month and latitude? Well, that is to be discussed in another post! Stay tuned!