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

Wendy #8

Open
wants to merge 3 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
265 changes: 265 additions & 0 deletions Wendy/assignment2Answers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
Question 1A.

function fractionString(n,d)
{
var a = Math.floor(n/d);
var b = (n-(d*a));
return a + ' ' + b + '/' + d;
}
console.log (fractionString(7,4));

Question 1B.

function fractionString(n,d) {

var a = Math.floor(n/d); //gives the whole number.
var b = (n-(d*a)); // gives the number that will be the new numerator in the smaller fraction
var c = (n%d); //provides a check to see if the fraction is actually a whole number.

if (c===0) { //This prevents the fraction with 0 as the numerator.
return a;
}

if (a > 0) { //This is the main outcome-turning improper fraction into proper fraction.
return a + ' ' + b + '/' + d;
}


else { // This is if you already have a proper fraction (i.e. numerator < denominator)
return n + '/' + d;
}
}
console.log(fractionString(n,d));

Choose a reason for hiding this comment

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

Nailed it! Well done.

// fractionString(8,4);
// fractionString(7,4);
// fractionString(5,10);

Question 2.

2a.

ORIGINAL:
var i;
if ((n - Math.floor(n)) >= .5) {
i = Math.ceil(n);
} else {
i = Math.floor(n);
}

//Math.ceil returns the smallest integer greater than or equal to the given number. eg. 2.4 = 3;
//Math.floor returns the smalelst integer less than or equal to the given number. eg. 2.4 = 2;
//for my own purposes, setting var i =6;
// ORIGINAL WITH WORKING NUMBERS:
// var i;
// var n = 3.2;
// if((3.2- 3))>= .5 {
// i = 4;
// }
// else {
// i = 3;
// }

//In other words, we are rounding up.
2a. ANSWER:
var i;
i = Math.round(n);

2b.

ORIGINAL:
var y, Banana = (x? false : true);//Does x evaluate to true? then answer will be false.
if (Banana) // value of x is false
y = false
else // if xIsFalse value is false:
y = x

//This is so many double negatives... I replaced "xIsFalse" with Banana in my tests.
//Ternary operator: points to remember, if the value on the left of the ? is TRUE, then the value in the middle is chosen.
//if it is FALSE, the value on the right is chosen.
// Also of note is that the value of xIsFalse will change depending on what the value of x is.

2b. Option 1:
//It seems that if x is true, y is also true and if x is false, y is also false.
var y, x;
y = x;

2b. Option 2:
//write it in ternary expression...I wasn't sure if y'all wanted it to stay as a ternary operator.
y = (x? y=false: y=x)

Choose a reason for hiding this comment

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

Ha. I like the switch to Banana. Way more readable.

ORIGINAL:
2c.
for (var count = 15 - i; count < 15; count = count + 1) {
i = i-1;
console.log(i+1)//this doesn't alter the for loop--it's a result.
}

2c. My Solution:
for (var i = 14; i > 0; i--){
console.log(i);
}


ORIGINAL:
2d.

}
```
**d)**
```
var x;
if (a) {
if (b) {
x = 0;
} else {
x = 1;
}
} else {
if (b) {
x = 1;
} else {
x = 2;
}
}

2d. My Solution:
if (a&&!b)||(b&&!a) {
x=1;
}
else if (a&&b) {
x=0;
}
else if (!a&&!b){
x=2;
}


PROBLEM 3.
var letterArray=['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'];


function letterTriangle(n) {
var output= '';
for (var line = 0; line<n ; line++) {
//defines how many times we do this line//
for ( var letter= line; letter>=0; letter--) {
//single step is going to be n * n times//
output= output + letterArray[letter];
}
output +='\n';
}
return output;
}
console.log(letterTriangle(4));

Choose a reason for hiding this comment

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

Nice work on this one, Wendy!

//I realize we were supposed to write the whole thing, but I was SO pressed for time this week that I just did the first six--trying to get the spirit of the exercise.
PROBLEM 3B.
var gifts= ['a Partridge in a Pear Tree', 'Two Turtle Doves and', 'Three French Hens', 'Four Calling Birds', 'Five Gold Rings', 'Six Geese a Laying'];
var day = ['On the first', 'On the second', 'On the third', 'On the fourth', 'On the fifth', 'On the sixth'];
// var output= '';

function twelveDays(n) {
var output='';
for ( var line = n; line>=0; line--) {//1. for loop to show how many times we do the line.
for (var verse= 0; verse<=line; verse++ ) {

output = '\n' + gifts[verse] + output;

}
// output = output + ' day of Christmas my true love gave to me ' + '\n' + day[line];
output = '\n' + day[line] + ' day of Christmas my true love gave to me: ' + output + '\n';
}
return output;
}
console.log(twelveDays(6));

Choose a reason for hiding this comment

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

Excellent job on this one as well!!


PROBLEM 4a.

function and2(a,b) {
var result = '';

if (a) {
if (b) {
result= true;
}
}
else {
result= false;
}


return result;
}

console.log(and2(10<6, true));

4b. SOLVING without a nested func.

function and3(a,b,c) {
var result = '';

if (a) { //if a is true, check to see if b is true.
if (b) {
if (c) {
result= true;
}
}
}//if b is true, check to c if c is true, if it is, then return true. Otherwise return false.
else {
result= false;
}

return result;
}

console.log(and3((6<3), false, true));

4b. Solving attempt with nested func.

var result = '';

function and3(a,b,c){
var result = '';
function and2(a,b) {


if (a) {
if (b) {
result= true;
}
}
else {
result= false;
}

return result;
}
console.log(result);//just checking to see if my inner loop was working//




if (c) {
if (and2) {
return true;
}
else {
return false;
}
}

return result;
}
console.log('bon jour');


console.log(and3(false, true, true));

4c. //Didn't get a chance to work on this. ):
4d. //Ditto.

5.// Going to try to tackle this and submit tomorrow. Sorry.


93 changes: 93 additions & 0 deletions Wendy/cards1a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Simple version (no error-detection)

// function()--> possible return values



//Build 4 arrays: hearts, with indeces 0, 4, 8, 12 etc. heart[0] would return ace of hearts
//id # 26 would be 7 of clubs
//THE SUIT ARRAYS//
var hearts=[0,4,8,12,16,20,24,28,32,36,40,44,48];
var diamonds=[1,5,9,13,17,21,25,29,33,37,41,45,49];
var spades=[2,6,10,14,18,22,26,30,34,38,42,46,50];
var clubs=[3,7,11,15,19,23,27,31,35,39,43,47,51];

//THE

//THE COLORS//
var red= new Array (hearts, diamonds);
var black= new Array (spades, clubs);
//THE CARDID//
var ace = [0,1,2,3];
var two = [4,5,6,7];
var three = [8,9,10,11];
var four = [12,13,14,15];
var five = [16,17,18,19];
var six = [20,21,22,23];
var seven = [24,25,26,27];
var eight = [28, 29, 30, 31];
var nine = [32, 33, 34, 35];
var ten = [36, 37, 38, 39];
var eleven =[40, 41, 42, 43];
var twelve = [44, 45, 46, 47];
var thirteen = [48, 49, 50, 51];

var deck = [ace, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen];

var ace = 3;
var two = 7;
var three= 11;
var four=

function rank(card) { // --> 1..13
for (var i = 51; i-4 > card; i-4) {




else if (card/4 < i) {
return 'one';
}
}

}

}
//var cardRank = ???
//return cardRank which is a number between 0 and 51


}
}

function suit(card) { // --> 1..4
}

function cardID(rank,suit) { // --> 0..51
}

function color(card) { // -->"red","black"
}

function name(card) { // --> string
}


// TESTING:
function assert(claim,message) {
if (!claim) console.error(message);
}
assert(rank(0)===1, "Test 1 failed");
assert(rank(3)===1, "Test 2 failed");
assert(rank(51)===13,"Test 3 failed");
assert(suit(0)===1, "Test 4 failed");
assert(suit(5)===2, "Test 5 failed");
assert(suit(51)===4, "Test 6 failed");
assert(cardID(1,1)===0, "Test 7 failed");
assert(cardID(13,4)===51, "Test 8 failed");
assert(cardID(8,3)===30, "Test 9 failed");
assert(color(0)==='red', "Test 10 failed");
assert(color(2)==='black', "Test 11 failed");
assert(name(5)==='Two of Diamonds', "Test 12 failed");
assert(name(51)==='King of Clubs', "Test 13 failed");