Skip to content

Instantly share code, notes, and snippets.

@bytezen
Created February 14, 2017 20:23
Show Gist options
  • Save bytezen/524bee0e29b0dca3e583a9c16a680546 to your computer and use it in GitHub Desktop.
Save bytezen/524bee0e29b0dca3e583a9c16a680546 to your computer and use it in GitHub Desktop.
IML400-Magic Card Trick Getting Started
//the number between 1-27
var favoriteNumber = 10;
// a list of 27 cards; here I am using
// S = spades, C = clubs, D = diamonds, H = hearts
var cards = ["2C","3C","4C",
"5C","6C","7C",
"8C","9C","10C",
"2S","3S","4S",
"5S","6S","7S",
"8S","9S","10S",
"2H","3H","4H",
"5H","6H","7H",
"8H","9H","10H"]
// convert a decimal number (base 10) to a
// base 3 number
function convertToBase3(aNumber) {
if( aNumber > 26) {
console.log('this function only works for numbers between 1 and 27')
return "000"
}
if( aNumber < 1) {
console.log('this function only works for numbers between 1 and 27')
return "000"
}
//how many times does 9 go into our number. Stated another way
// how many times can we take 9 away from our number?
var nines = Math.floor(aNumber / 9)
// Ok, now take that many number of nines away and see how much we have left over
var ninesRemainder = aNumber - (nines * 9)
//Now how many times can we take 3 away from the remainder?
var threes = Math.floor( ninesRemainder / 3)
// Take that number of 3's away from the remainder
var threesRemainder = ninesRemainder - (threes * 3)
//Finally what is remaining is the number that will go in the 1's column
//This is the remainder after we have taken away all the 9's and the 3's
// To be explicit this is what we are doing
var ones = Math.floor(threesRemainder / 1)
//Now we smash together the nines, threes and ones to make our base 3 Number
//We are going to store it as a string NOT a number. This is just like we store
// Hexadecimal numbers for colors: "#AB108C" for instance
var baseThreeNumber = "" + nines + threes + ones
return baseThreeNumber
}
// given a base 3 number get the number that appears in the 9's column.
// For example given the base 3 number 021 or 21, this function would return 0
function getThe9Column(aBase3Number) {
if(aBase3Number.length > 3) {
console.log('this function only works for base3 numbers that are 3 digits long or less')
return 0;
}
if(aBase3Number.length < 3) {
return 0
} else {
return aBase3Number.substr(0,1);
}
}
// given a base 3 number get the number that appears in the 3's column.
// For example given the base 3 number 021 or 21, this function would return 2
function getThe3Column(aBase3Number) {
if(aBase3Number.length > 3) {
console.log('this function only works for base3 numbers that are 3 digits long or less')
return 0;
}
if(aBase3Number.length < 2) {
return 0
} else {
return aBase3Number.substr(1,1);
}
}
// given a base 3 number get the number that appears in the 1's column.
// For example given the base 3 number 021 or 21, this function would return 1
function getThe1Column(aBase3Number) {
if(aBase3Number.length > 3) {
console.log('this function only works for base3 numbers that are 3 digits long or less')
return 0;
}
if(aBase3Number.length < 1) {
return 0
} else {
return aBase3Number.substr(2,1);
}
}
/* to test it out:
Open Chrome Developer Tools:
View >> Developer >> Javascript Console
and paste this file into the console.
To test the function just enter the following into the console
*/
console.log("Converting the number " + favoriteNumber + " to base 3 = ")
var result = convertToBase3(favoriteNumber)
console.log( result )
//if you want to test different numbers just enter the following into the console
console.log( convertToBase3(27) )
console.log( getThe9Column("210") )
console.log( getThe3Column("210") )
console.log( getThe1Column("210") )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment