Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 54 additions & 10 deletions cards4-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,39 @@ var makeCard = // receive factory with external name `makeCard`

Copy link

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.

// 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'],
Copy link

Choose a reason for hiding this comment

The 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...
};



//--------------------------
Expand All @@ -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]);
Copy link

Choose a reason for hiding this comment

The 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...


Expand All @@ -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;
129 changes: 107 additions & 22 deletions deque2-template.js
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;
})();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

// Part b): Turn this file into an IIFE module!
44 changes: 42 additions & 2 deletions users-template.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
var makeUser = (function() {// begin IIFE...

var sharedLog = []; //private; accessible only from functions defined within IIFE


// The factory itself:
function makeUser(name,passwd) {

var user = {
getName : getName,
validate : validate,
record : record,
};

function getName(){
return name;
}
function validate(str){
if (str === passwd){
return true;
} else {
return false;
}
}


return user;
// Return a user object with three methods:
// getName()
// validate(str)
// record(message) (Part b)
}

function record(message){
if (message){
sharedLog.push(this.getName() + ':' + message);
return true;
}
}
// Part b) only:
// Factory method (defined within IIFE, so can access sharedLog):
makeUser.getLog = function(user) {
}

function messageCallback(item){
if (item.indexOf(user.getName())>=0){
return true;
}
}

if (user){
var userLog = sharedLog.filter(messageCallback);
return userLog.join('\n');
} else {
return sharedLog.slice().join('\n');
}
};


return makeUser;
})();
Expand Down