Elchanan Mossel’s Probability Problem in JavaScript

An interesting problem popped into my twitter feed this morning:

Here’s how I wrote the problem in JavaScript:

function rollDie() {
  return Math.floor(Math.random() * (7 - 1) + 1);
}

function allEven(arr) {
  for (var i = 0; i < arr.length; i += 1) {
    if (arr[i] % 2 !== 0) {
      return false;
    }
  }

  return true;
}

function rollsToSix() {
  var rollResults = [];
  var sixRolled = false;

  while (sixRolled === false) {
    var roll = rollDie();

    if (roll === 6) {
      rollResults.push(6);
      sixRolled = true;
    } else {
      rollResults.push(roll);
    }
  }

  console.log(rollResults);
  return rollResults;
}

function howManyPositiveResults(attempts) {
  var positiveResults = 0;

  for (var i = 0; i < attempts; i += 1) {
    var resultOfRoll = allEven(rollsToSix());

    if (resultOfRoll === true) {
      positiveResults += 1;
    }
  }

  return positiveResults;
}

console.log(howManyPositiveResults(100));

The above program runs the results 100 times and returns the number of positive results. It also logs all the rolls even if the consecutive rolls included odd numbers.

It was fun to watch Mike Lawler do this problem with his children. I learned another approach to the problem from him. You can watch it here:

Exploring Elchanan Mossel’s fantastic probability problem with kids

Bootcamp Prep – The Videos I Wish I Had When I Started

Coding Bootcamp Prep Course
The view from the App Academy building… awash in code.

It’s challenging to prepare for the technical interview application process at immersive coding
bootcamps like App Academy and Hack Reactor.

I’m trying to make it easier… here’s a collection of videos that I think make it easier.

Creation Story

For most of 2016, I was working hard to learn the needed coding skills to pass the technical interviews. My goal was to get accepted to the top programming bootcamps in San Francisco.

In July of 2016, I applied to MakerSquare, Hack Reactor and App Academy.

I was accepted into Hack Reactor and App Academy, both of which are considered very challenging schools to get into.

After a lot of deliberation I chose to be a part of the November, 2016 cohort at App Academy.

At the time of writing this, I have 2 months until the start of the cohort so I want to spend the time teaching others how I think about the technical challenges and provide material to speed the skill acquisition of future aspiring programmers.

Tools

There are a lot of tools to get hung up on. If you follow along with the video course, you can skip that.

In this course we will use Node and a simple text editor. Both are easy installs.

Programmers are crazy about their tools and that is a good thing. We’re learning the basics so we don’t need the fancy stuff. Fancy tools will actually hurt you during your coding interview because they become a crutch.
During many interviews, you won’t be able to run your code or write in an editor that gives you hints on syntax. We’re doing this spartan style so you have the core ability to pass the technical interview no matter what tools they ask you to program in.

Language

We will be using JavaScript. Here’s why:

JavaScript is used to build almost all websites these days.

Even if you’re going to a school that focuses on Ruby or Python, you will still find yourself using JavaScript (or it’s derivatives) to display the results of your code on a website.

For that reason, it makes sense to learn JavaScript first. The syntax is more challenging (IMHO) and therefore it is better to start there. Once you get good at JavaScript, Ruby and Python are easy to pick up. I transitioned from Ruby to JavaScript and I think it would have been easier to go the other way.

When I started, I learned Ruby. After transitioning to JavaScript, I wish I had started there and moved to Ruby later.

I hope you find this useful.

You can download the course material as I create it via this GitHub repository.