# Pico Fermi Bagels with JavaScript and Math

I have a friend that's taking coding classes from The Tech Academy here in Portland. Having taught a 10-week intro to web development course through Mount Hood Community College and beginning programming courses to kids through Saturday Academy, I'm always curious how other places go about teaching these concepts. It's also fun to hear about the exercises and challenges they're given to try and keep my own skills sharp!

Right now he's been working with Python and decided to try and recreate a math game called Pico Fermi Bagels. I'd never heard of this game! For whatever reason, despite having a little bit of a knack for programming, I never had much of a propensity for math when I was young and missed out on a lot of fun games and exercises that I've become more interested in later in life.

The rules for Pico Fermi Bagels are as follows: One person — the "leader" — comes up with a random 3 or 4 digit number and the other person tries to guess. For each guess, the leader gives clues:

**Pico**: One of the numbers is correct, but it's in the wrong place.**Fermi**: One of the numbers is correct AND is in the right place.**Bagel**: Nothing is correct.

So if the number was 462 and someone's guess was 260 the clues would be: Pico, Fermi, Bagel. Though there's nothing that forces the leader to give the clues in the order the numbers were given, I'm fairly certain.

I like simple games like this because they're fun to recreate in the programming language of your choice, and they also make your brain work in a slightly different way. My inner mentor — somewhat obnoxiously I imagine — tries to give subtle hints or advice when my friend shares this information with me. For this one I said I'd probably try to use arrays and effectively treat the numbers like strings. In a pseudo-JavaScript-code, something like:

```
let number = [4, 6, 2];
let guess = [2, 6, 0];
for (i in guess) {
let position = number.indexOf(guess[i]);
if (position > -1 && i == position) {
console.log("Fermi");
}else if (position > -1 && i !== position) {
console.log("Pico");
}else{
console.log("Bagel");
}
}
// Output should be: Pico, Fermi, Bagel
```

Arrays are perfectly suited to do this sort of thing as we only care about the value and position — the fact that it's a number doesn't really have any bearing on anything. We could just as easily be playing this game with emoji — which would be kind of a fun, surrealist take on it — and use the exact same code for a 1-digit number as an 8-digit number.

But my friend, having a more thorough math background than I do, decided to try and figure out an approach using a more strictly math-based way. This intrigued me because I wasn't sure how to do that.

I knew we would need a function to figure out the number in each position: ones, tens, hundreds, etc. I knew we'd probably want to use the modulus operator (%) to return the remainders.

My friend sent along his example with a separate formula for finding the ones, tens and hundreds digit. I refactored his example to make a function in JavaScript that should be usable to find the digit in a number of any size using a more mathematical approach than my array-based one:

```
function digits(number, place) {
if (Math.pow(10, place - 1) > number) return false;
return Math.floor (number % Math.pow(10, place) / Math.pow(10, place - 1));
}
```

To see it in action:

```
place = 1; // start with the ones place
number = Math.round( Math.random() * 100000000 );
console.log("Number:", number);
while ( digits(number,place) !== false ) {
console.log( "Digit #" + place + ": " + digits(number, place) );
place++;
}
```

In my testing it seemed to work for a number of any size. I'd be curious to compare it to my array-based approach and see if it's any faster.

Now with the tricky part done I just need to make the rest of the game!

If you'd like to use this little bit of JavaScript to build your own version of Pico Fermi Bagels — or for any other reasons — please check it out on GitHub: