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

homework 2 #5

Open
wants to merge 2 commits 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
20 changes: 20 additions & 0 deletions cards-template1a.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@

Copy link

Choose a reason for hiding this comment

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

Remember when cloning the repository in git to create your own folder (named after yourself!) to store your own homework solutions in.

// function()--> possible return values

Copy link

Choose a reason for hiding this comment

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

Looks good! Here's a tip for simplification: Instead of creating a variable that contains your equation and then returning that variable, you can drop the variable (rankOfCard, for example) and just return the equation as it is.

var card;


function rank(card) { // --> 1..13
var rankOfCard = ((card-(card%4))/4)+1;
return rankOfCard;
}

function suit(card) { // --> 1..4
var suitOfCard = (card%4)+1;
return suitOfCard;
}


function cardID(rank,suit) { // --> 0..51
var cardIDfound = (4*(rank-1)+(suit-1));
return cardIDfound;
}

function color(card) { // -->"red","black"
var column = (card%4);
if (column<=1) {
return 'red';
} else {
return 'black';
}
}

function name(card) { // --> string
var ranks = ['Ace','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King'];
var suits = ['Hearts','Diamonds','Spades','Clubs'];
var nameOfCard = ranks[(card-(card%4))/4] + ' of ' + suits[(card%4)];
return nameOfCard;
}


Expand Down
53 changes: 52 additions & 1 deletion cards-template1b.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,75 @@
// Error-detecting version
Copy link

Choose a reason for hiding this comment

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

You've got a fair amount of this working well, but after running some of the later (Number.isNan) assertions, we run into some problems. When checking for validity of your arguments in these functions, remember that we are not only checking to see if the number is between 0-51, but also if the argument given is a number at all.

While thinking about that, consider also whether you could make a helper function that checks the validity of the argument given to your functions, that replaces copying/pasting the same line(s) of code into each function.

You're on the right path here, keep pressing forward!


function rank(card) {
var rankOfCard = ((card-(card%4))/4)+1;
if (((card%1)===0)&&(card<=51)){
return rankOfCard;
} else {
return NaN;
}

}

function suit(card) {
var suitOfCard = (card%4)+1;
if (((card%1)===0)&&(card<=51)){
return suitOfCard;
} else {
return NaN;
}
}

function cardID(rank,suit) {
var cardIDfound = (4*(rank-1)+(suit-1));
if (((rank%1)===0)&&(0<rank<=13)){
return cardIDfound;
} else if (((suit%1)===0)&&(0<suit<=4))
{return cardIDfound;
} else {
return NaN;
}
}

function color(card) {
if (((card%1)===0)&&(card<=51)){
var column = (card%4);
if (column<=1) {
return 'red';
} else {
return 'black';
}
} else {
return NaN;
}

}

function name(card) {
var ranks = ['Ace','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King'];
var suits = ['Hearts','Diamonds','Spades','Clubs'];
if (((card%1)===0)&&(card<=51)){
var nameOfCard = ranks[(card-(card%4))/4] + ' of ' + suits[(card%4)];
return nameOfCard;
} else {
return NaN;
}
}



// TESTING:
function assert(claim,message) {
if (!claim) console.error(message);
}

//my tests!
assert(color(38)==='black', "test A failed");
assert(suit(14)===3, "test B failed");
assert(name(42)==="Jack of Spades", "test C failed");
assert(rank(23)===4, "good, test D failed!");
assert(color(20)==='black', "good, test E failed!");
assert(cardID(3,2)===34, 'good, test F failed!');

//
assert(rank(0)===1, "Test 1 failed");
assert(rank(3)===1, "Test 2 failed");
assert(rank(51)===13,"Test 3 failed");
Expand Down Expand Up @@ -66,3 +115,5 @@ assert(Number.isNaN(name(-1)), "Test 44 failed");
assert(Number.isNaN(name(52)), "Test 45 failed");
assert(Number.isNaN(name(NaN)), "Test 46 failed");



105 changes: 105 additions & 0 deletions homework2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//Section 1
//a
function fractionString(n,d){
var n;
var d;
var remainder = n % d;
var wholeNumber = ((n - remainder)/d);
var result = wholeNumber + " " + remainder + '/' + d;
return result;
}

console.log(fractionString(3,3));

//b
function fractionString(n,d){
var n;
var d;
var remainder = n % d;
var wholeNumber = ((n - remainder)/d);
var result = wholeNumber + " " + remainder + '/' + d;
var noWholeNumber = remainder + '/' + d;
if (wholeNumber===0){
return noWholeNumber;
} else if (remainder===0){
return wholeNumber;
} else {
return result;}
}

console.log(fractionString(4,2));

//Section 2
//a
var i = math.round(n);

//b
var y = (x? false : true);

//c
var i=15;

for (i; i>0; i--){
console.log(i);
}

//d
if (a&&b){
x = 0;
}else if (a||b){
x = 1;
}else {
x = 2;
}

//3

//a
var letter = ['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 n;

function letterTriangle(n){
var output = '';
for (var line =0; line<n; line++){
for (var col=line; col>=0; col--){
output = output + letter[col];
}
output += '\n';
}
return output;
}

console.log(letterTriangle(5));

Choose a reason for hiding this comment

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

Nice job on this one 👍

//b coming soon!

Choose a reason for hiding this comment

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

Let me know if you need any help figuring this one out!

// Section 4

//a
function and2(a,b){
if (a = true){
}else if (b = true) {
return true;
}else {
return false;
}
if (a = false){
}else if (b = false){
return true;
}else {
return false;
}

}

Choose a reason for hiding this comment

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

You're on the right track here! Basically, if a is truthy, you want to return b, otherwise you want to return a.

function and2(a,b) {
if (a) {
return b;
} else {
return a;
}
}

//hmmm not quite working but not sure why??
//more coming soon! sorry :(