Skip to content

Week 3 class assignment Joann Cahill #8

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

Open
wants to merge 8 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
31 changes: 30 additions & 1 deletion battleGame.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
// 1. Create attack function below. This will take the following parameters:
// attackingPlayer, defendingPlayer, baseDamage, variableDamage

// const attack2 = function (attackingPlayer, defendingPlayer, baseDamage, variableDamage) {

// const randomValue = Math.floor(Math.random() * variableDamage);
// const totalDamage = baseDamage + randomValue;
// defendingPlayer.health = defendingPlayer.health - totalDamage;

// console.log( `${attackingPlayer.name}' hits '${defendingPlayer.name} 'for '${totalDamage}' damage'`)
// }


// 2. Create player1 and player2 objects below
// Each should have a name property of your choosing, and health property equal to 10

let player1 = {
name: 'Joe',
health: 10,
};

let player2 = {
name: 'Jane',
health: 10,
};

// console.log(player1)
// console.log(player2)

// 3. Refactor attack function to an arrow function. Comment out function above.
const attack = (attackingPlayer, defendingPlayer, baseDamage, variableDamage) => {
const randomValue = Math.floor(Math.random() * variableDamage);
const totalDamage = baseDamage + randomValue;
defendingPlayer.health = defendingPlayer.health - totalDamage;

// console.log( randomValue );
// console.log( totalDamage );
// console.log( defendingPlayer.health );
let gameResult = (`${attackingPlayer.name}' hits '${defendingPlayer.name} 'for '${totalDamage}' damage'`);
return gameResult;
}



Expand All @@ -23,7 +53,6 @@ while (player1.health >= 1 && player2.health >= 1 && preventInfiniteLoop > 0) {
const [attackingPlayer, defendingPlayer] = attackOrder;
console.log(attack(attackingPlayer, defendingPlayer, 1, 2));
attackOrder = attackOrder.reverse();

preventInfiniteLoop--;
}
const eliminatedPlayer = player1.health <= 0 ? player1 : player2;
Expand Down
62 changes: 55 additions & 7 deletions itemizedReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,62 @@



// Check
logReceipt(
{ descr: 'Burrito', price: 5.99 },
{ descr: 'Chips & Salsa', price: 2.99 },
{ descr: 'Sprite', price: 1.99 }
);
// Create an array of menu item objects
const items = [{
descr: 'Burrito',
price: 5.99
},
{
descr: 'Chips & Salsa',
price: 2.99
},

{
descr: 'Mexi Fries',
price: 1.99
},

{
descr: 'Sprite',
price: 1.99
}

]

function logReceipt() {
//set total cost to 0
let subTotal = 0;

//create list of items purchased and their price
items.forEach(item => console.log(`${item.descr} - $${item.price}`))

//calculate total of all items purchased
items.forEach(item => subTotal += item.price);

//calculate tax
const tax = parseFloat((subTotal * .01).toFixed(2));

//calculate total with tax
total = parseFloat((subTotal + tax).toFixed(2));

//return totals and tax
const displaySub = (`Subtotal - ${subTotal}`);
const displayTax = (`Tax - ${tax}`);
const displayTotal = (`Total Cost - ${total}`);

console.log (displaySub);
console.log(displayTax);
console.log(displayTotal);


};

logReceipt();
Copy link
Contributor

Choose a reason for hiding this comment

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

Unfortunately by undoing the bit of boilerplate that tests the logReceipt() function we sort of lose our way as to what is being exercised. The implementation above has become brittle and not re-usable . . it's a one-use function that entirely relies upon some external state named items . . . What we're trying to get to is a function that can be passed any number of menu item objects (as the beginning file has as its test case)

Try again, and be sure that the logReceipt function can take an undetermined number of inputs . . . . remember the 'spread' operator!




// should log something like:
// Burrito - $5.99
// Chips & Salsa - $2.99
// Sprite - $1.99
// Total - $10.97
// Total - $10.97
42 changes: 40 additions & 2 deletions phoneNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,54 @@
// $ end of line

// check testPhoneNumber

console.log('Result for validation of phone number')

function testPhoneNumber(phone) {
const regex = /^\(\d{3}\)[-\s]\d{3}[-\s]\d{4}$/;
//validate whether phone number matches regex format
if (phone.match(regex)) {
return ('true');
} else {
return ('false');

}

};
//run function with parameters
console.log('Phone number is valid')
console.log(testPhoneNumber('(206) 333-4444')); // should return true
console.log('Phone number is not valid')
console.log(testPhoneNumber('(206) 33-4444')); // should return false, missing a digit


// 1. Create a function parsePhoneNumber that takes in a phoneNumber string
// in one of the above formats. For this, you can *assume the phone number
// in one of the above fparsePhoneNumberormats. For this, you can *assume the phone number
// passed in is correct*. This should use a regular expression
// and run the exec method to capture the area code and remaining part of
// the phone number.
// Returns an object in the format {areaCode, phoneNumber}
console.log('Result for parsing of phone number');

function parsePhoneNumber(phone) {

//parse area code
const regexAreaCode = /\d{3}/g;
let parsedAreaCode = regexAreaCode.exec(phone);
// console.log(parsedAreaCode);


//find phone nubers
const regexPhone = /\d{3}[-\s]\d{4}/;
let parsedPhone = regexPhone.exec(phone);
let parsedPhone2 = parsedPhone[0].replace('-', '');
// console.log(parsedPhone);
// console.log(parsedPhone2);

//create format to be used in return statement
let phoneNumbers = (`areaCode: ${parsedAreaCode}, phoneNumber: ${parsedPhone2} `)
Copy link
Contributor

Choose a reason for hiding this comment

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

Very near the mark, but this is an important distinction . . . the goal is to return an object with the 2 values . . . here we are returning a string! I know it's a tad trivial seeming with this exercise, but having keen intentionality of types is a powerful skill to hone.

Copy link
Contributor

Choose a reason for hiding this comment

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

refer to lines 75 and 78 for a glimpse of how the return value should look.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also worth mentioning, this use of parenthesis around the string is not necessary

// console.log(phoneNumbers);
return phoneNumbers;
};



Expand Down
47 changes: 42 additions & 5 deletions soccer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const RESULT_VALUES = {
w: 3,
d: 1,
Expand All @@ -16,27 +15,65 @@ const getPointsFromResult = function getPointsFromResult(result) {
return RESULT_VALUES[result];
}


// Create getTotalPoints function which accepts a string of results
// including wins, draws, and losses i.e. 'wwdlw'
// Returns total number of points won

function getTotalPoints(gameResults) {
//split the parameter of wins, losses, draws into separate values
let gameResultsSplit = gameResults.split('');
//calculate points for the parameter
let total = 0;
//add the points value to the total if the gameResult is part of the valid result values
gameResultsSplit.forEach((gameResult) => {
if (RESULT_VALUES[gameResult]) {
total += RESULT_VALUES[gameResult];
}

})
//console.log(total);
return total

}


// Check getTotalPoints
console.log(getTotalPoints('wwdl')); // should equal 7
console.log(getTotalPoints('wwldlwd')); // should equal 11
console.log(getTotalPoints('wzldlwd')); // should equal 8

// create orderTeams function that accepts as many team objects as desired,
// each argument is a team object in the format { name, results }
// i.e. {name: 'Sounders', results: 'wwlwdd'}
// Logs each entry to the console as "Team name: points"

//added for each loop sp function will loop thru multiple game results
function orderTeams(...gameResults) {
gameResults.forEach((gameResult) => {
let gameResultsSplit = gameResult.results.split('');
let total = 0;
gameResultsSplit.forEach((gameResult) => {
if (RESULT_VALUES[gameResult]) {
total += RESULT_VALUES[gameResult];
}
})
console.log(`name: ${gameResult.name}, results: ${total}`);
})
}


// Check orderTeams
orderTeams(
{ name: 'Sounders', results: 'wwdl' },
{ name: 'Galaxy', results: 'wlld' }
);
orderTeams({
name: 'Sounders',
results: 'wwdl'
}, {
name: 'Galaxy',
results: 'wlld'
}, {
name: 'Minnesota United',
results: 'dldw'
});
// should log the following to the console:
// Sounders: 7
// Galaxy: 4
19 changes: 19 additions & 0 deletions spaceShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,26 @@
// - should have a method accelerate that logs to the console
// `${name} moving to ${topSpeed}`

class SpaceShip {
constructor(name, topSpeed) {
this.name = name;
this.topSpeed = topSpeed;

}

usetopSpeed(){
const{name, topSpeed} = this;
Copy link
Contributor

Choose a reason for hiding this comment

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

Aha! Yes, this destructuring from this is a great strategy for methods, it makes them quite readable, BUT . . . . . .

console.log( `${this.name} moving to ${this.topSpeed}`)
Copy link
Contributor

Choose a reason for hiding this comment

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

if we don't use the destructured values, it becomes moot. You can probably see in VSCode that name and topSpeed on line 14 are kind of greyed out, or you can hove and see they are 'declared but never read' . . . . and that is because on line 15 you are accessing using this. .. . Now either way is totally valid, but it's a one-or-the-other kind of thing 😉

}

}


// 2. Call the constructor with a couple ships,
// and call accelerate on both of them.

const spaceX = new SpaceShip('Dragon', '5,770 mph');
spaceX.usetopSpeed();

const blueOrigin = new SpaceShip('New Shepard', '2,217 mph');
blueOrigin.usetopSpeed();