-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Elizabeth's homework 5 #3
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,39 @@ var makeCard = // receive factory with external name `makeCard` | |
|
||
// The factory itself: | ||
function makeCard(id) { //makeCard is also IIFE's internal name | ||
// set instance properties here | ||
if (makeCard.isValidID(id)){ | ||
return { | ||
|
||
id: id, | ||
rank: rank, | ||
suit: suit, | ||
color: color, | ||
name: cardName | ||
|
||
} | ||
} else{ | ||
return null; | ||
} | ||
}; | ||
|
||
|
||
// //----------------------- | ||
// // Methods to be called through factory only: | ||
// //----------------------- | ||
makeCard.isValidID = function(id){ | ||
return (typeof id === "number")&&(id%1===0)&&(id>=0)&&(id<=51); | ||
}; | ||
|
||
makeCard.isCard = function(thing) { | ||
return (typeof thing === 'object')&&(makeCard.rank === thing.rank)&&('id' in thing)&&(makeCard.isValidID(thing.id)); | ||
}; // set instance properties here | ||
|
||
var rankNames = ['','Ace','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slight formatting error on this line (33, and again on line 35), replace your 's at the end of the arrays with ;'s. |
||
|
||
var suitNames = ['','Hearts','Diamonds','Spades','Clubs'], | ||
//... | ||
// and return instance... | ||
}; | ||
|
||
|
||
|
||
//-------------------------- | ||
|
@@ -21,13 +50,28 @@ var makeCard = // receive factory with external name `makeCard` | |
//----------------------- | ||
// Instance Methods: | ||
//----------------------- | ||
|
||
function rank() { | ||
} | ||
|
||
function suit() { | ||
} | ||
|
||
rank = function() { | ||
return Math.floor(this.id/4)+1; | ||
// // --> 1..13 | ||
// // code here... | ||
}; | ||
|
||
suit = function() { | ||
return ((this.id%4)+1); // --> 1..4 | ||
// // code here... | ||
}; | ||
|
||
color = function() { | ||
var theSuit=this.suit(); //may be NaN | ||
return theSuit && ((theSuit<3)? "red": "black"); | ||
}; | ||
|
||
cardName = function() { | ||
var theRank = this.rank(); | ||
var theSuit = this.suit(); | ||
return theRank && theSuit && | ||
(makeCard.rankNames[theRank]+' of '+makeCard.suitNames[theSuit]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 73 in its current state is causing an error in the cardName function. If you run this function at the moment it will give an error saying that (a number) cannot be determined of property undefined. The undefined property in this case is makeCard, and the reason for this is that your cardName function is actually living in the same level as your rankNames (and suitNames) array. This is what you want, but you don't need to reference the makeCard function for any sort of traversal. just rankNames and suitNames on their own are good enough. |
||
}; | ||
//etc... | ||
|
||
|
||
|
@@ -51,4 +95,4 @@ var makeCard = // receive factory with external name `makeCard` | |
// Export as node-style module, to make testing your code easier. | ||
// (If you don't understand this line, you may ignore it.) | ||
if (typeof module != 'undefined') | ||
module.exports = makeCard; | ||
module.exports = makeCard; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,124 @@ | ||
// Part a): modify factory to use non-shared (instance-specific) methods | ||
// which have access to private variables: | ||
var makeDeque = (function(){ | ||
function makeDeque(values) { | ||
|
||
function makeDeque(values) { | ||
|
||
// These vars are private, local to scope of makeDeque, | ||
// only accessible to functions defined in makeDeque: | ||
var array = values.slice(); //copy values | ||
var absent = []; //list of missing elements | ||
// These vars are private, local to scope of makeDeque, | ||
// only accessible to functions defined in makeDeque: | ||
var array = values.slice(); //copy values | ||
var absent = []; //list of missing elements | ||
|
||
|
||
// Each function below is specific to one deque, with access to the private vars | ||
// Each function below is specific to one deque, with access to the private vars | ||
|
||
// ---- Internal use only ---- | ||
function readmit(val) { | ||
//... | ||
} | ||
// ---- Internal use only ---- | ||
function readmit(val) { | ||
var valIndex = absent.indexOf(val); | ||
if (valIndex>=0){ | ||
absent.splice(valIndex,1); | ||
return true; | ||
} | ||
} | ||
|
||
// ---- Public instance methods: ----- | ||
// ---- Public instance methods: ----- | ||
|
||
function top() { | ||
} | ||
function top() { | ||
return array[array.length-1]; | ||
|
||
function bottom() { | ||
} | ||
} | ||
|
||
// etc... | ||
function bottom() { | ||
return array[0]; | ||
} | ||
|
||
function pop() { | ||
if (array.length===0) { | ||
return null; | ||
} | ||
var popped = array.pop(); | ||
absent.push(popped); | ||
return popped; | ||
} | ||
|
||
return { //one deque instance... | ||
top : top, | ||
bottom : bottom, | ||
//etc | ||
}; | ||
function push(val){ | ||
if (readmit(val)){ | ||
return array.push(val); | ||
} | ||
} | ||
|
||
} //end makeDeque | ||
function unshift(val) { | ||
if (readmit(val)){ | ||
return array.unshift(); | ||
} | ||
} | ||
|
||
function shift() { | ||
if (array.length===0) { | ||
return null; | ||
} | ||
var shifted = array.shift(); | ||
absent.push(shifted); | ||
return shifted; | ||
} | ||
|
||
function cut() {var middle; | ||
if (array.length<2) { | ||
return array; | ||
} else { | ||
middle = Math.ceil(array.length/2); | ||
} | ||
|
||
var halfA = array.slice(middle, array.length); | ||
var halfB = array.slice(0, middle); | ||
array = halfA.concat(halfB); | ||
|
||
} | ||
|
||
function map(convertValFn) { | ||
return array.map(convertValFn); | ||
} | ||
|
||
function sort(compareValsFn) { | ||
return array.sort(compareValsFn); | ||
} | ||
|
||
function inPlaceShuffle(array) { | ||
var m = array.length, t, i; | ||
|
||
// While there remain elements to shuffle… | ||
while (m) { | ||
|
||
// Pick a remaining element… | ||
i = Math.floor(Math.random() * m--); | ||
|
||
// And swap it with the current element. | ||
t = array[m]; | ||
array[m] = array[i]; | ||
array[i] = t; | ||
} | ||
|
||
return array; | ||
} | ||
// etc... | ||
|
||
|
||
return { //one deque instance... | ||
top : top, | ||
bottom : bottom, | ||
pop : pop, | ||
push: push, | ||
shift : shift, | ||
unshift : unshift, | ||
cut : cut, | ||
map : map, | ||
sort : sort, | ||
goodShuffle : inPlaceShuffle | ||
//etc | ||
}; | ||
|
||
} //end makeDeque | ||
return makeDeque; | ||
})(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice work! |
||
// Part b): Turn this file into an IIFE module! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, this is looking pretty good. Check below comments for some syntax issues and one function correction. If you have time, try to return to the makeCard.fullset and add functionality so it will create 52 cards; right now it's just creating an empty array.