Created
December 12, 2016 02:40
-
-
Save anonymous/e853c96b7477679cca57b20b056d440b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// map: scrabble -> createRack -> randNum | |
// map: scrabble -> checkWordInRack -> wordToLetters | |
function randNum(){ | |
// creates a random number | |
return Math.floor( Math.random() * 25); | |
}; | |
function createRack(){ | |
// initiates a two arrays. One filled with the abcs and an empty array for the rack | |
// Then it pushes seven letters into the rack | |
// calls ranNum() | |
// Then returns that rack | |
var abc = [ 'a', 'b','c', 'd', 'e','f','g', 'h','i', 'j', 'k', 'l', 'm','n','o', 'p', 'q', 'r', 's', 't' | |
,'u','v','w','x','y', 'z' ]; | |
var rack = []; | |
for(i=0;i<7;i++){ | |
rack.push(abc[randNum()]); | |
} | |
return rack; | |
}; | |
function wordToLetters(word){ | |
// gets a string | |
// trims it | |
// takes that cleaned string and puts every letter in an array by the split command | |
var clean = word.trim(); | |
return clean.split(""); | |
}; | |
function checkWordInRack(rack,word){ | |
// gets a word | |
// splits that word | |
// calls wordToLetter(word); | |
// creates an new rack | |
// calls createRack(); | |
// then, for each it checks the index in the rack array | |
// then it checks the array of every letter in the rack array | |
// but, if it's false it returns "you cannot use this word" and breaks | |
var wordArray = wordToLetters(word); | |
console.log(word); | |
var checker = 0; | |
for(i=0; i<wordArray.length; i++){ | |
checker = rack.indexOf(wordArray[i]); | |
if(checker < 0){ | |
return "you cannot use this word" | |
break | |
} | |
else{ | |
checker = 0; | |
} | |
}; | |
return "you can use this word"; | |
//return "You can use this word."; | |
}; | |
function scrabble(){ | |
// creates a new Rack | |
// creates a word list | |
// outputs weather or not you can you the words from the list | |
var rack = createRack(); | |
var wordList = { | |
zit: checkWordInRack(rack, "zit"), | |
xis: checkWordInRack(rack, "xis"), | |
ant: checkWordInRack(rack, "ant") | |
} | |
console.log("your rack: " + rack); | |
console.log("zit:" + wordList.zit); | |
console.log("xis:" + wordList.xis); | |
console.log("ant:" + wordList.ant); | |
} | |
scrabble(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment