Skip to content

week 3 hw take 2 #7

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 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
18 changes: 18 additions & 0 deletions battleGame.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
// 1. Create attack function below. This will take the following parameters:
// attackingPlayer, defendingPlayer, baseDamage, variableDamage

//const attack = function (attackingPlayer, defendingPlayer, baseDamage, variableDamage) {
// const randomDamage = Math.floor(Math.random() * variableDamage);
// const totalDamage = baseDamage + randomDamage;
// defendingPlayer.health -= totalDamage;
// console.log(`${player1.name} hits ${player2.name} for ${defendingPlayer.health} damage`);
//}
//


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

player1 = { name : 'kelly',
health: 10 };

player2 = { name: 'mark',
health: 10 };

// 3. Refactor attack function to an arrow function. Comment out function above.

const attack = (attackingPlayer, defendingPlayer, baseDamage, variableDamage) => {
const randomDamage = Math.floor(Math.random() * variableDamage);
const totalDamage = baseDamage + randomDamage;
defendingPlayer.health -= totalDamage;
console.log(`${player1.name} hits ${player2.name} for ${defendingPlayer.health} damage`);
}



// DO NOT MODIFY THE CODE BELOW THIS LINE
Expand Down
15 changes: 15 additions & 0 deletions itemizedReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
// function should log each item to the console and log a total price


// const logReceipt = function(...menuItems) {
// then iterate over menuItems

const logReceipt = function(...items) {
let total = 0;
const tax = .1025;
items.forEach(function (item) {
total += item.price;
taxes = item.price * tax;
total += taxes;
console.log(`${item.descr} - ${item.price} with tax ${(item.price + taxes).toFixed(2)}`);
}
)
console.log(`Total with tax - ${total.toFixed(2)}`)
};

// Check
logReceipt(
Expand Down
17 changes: 15 additions & 2 deletions phoneNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
// '206 333 4444'
// Returns true if valid, false if not valid


const testPhoneNumber = function(phoneNumber) {
const testFormat = /^\(\d{3}\)?\s\d{3}[-\s]\d{4}$/;
const regex = new RegExp( testFormat );
return regex.test(phoneNumber);
};

// Explanation of RegExp
// ^ start of line
Expand All @@ -30,11 +34,20 @@ console.log(testPhoneNumber('(206) 33-4444')); // should return false, missing a
// the phone number.
// Returns an object in the format {areaCode, phoneNumber}

const parsePhoneNumber = function(phoneNumber) {

const regex1 = new RegExp(/(\d{3})/);
const areaCode = regex1.exec(phoneNumber);

const regex2 = new RegExp(/(\d{3})[\s-](\d{4})/);
const phone = regex2.exec(phoneNumber);

return (`areaCode: ${areaCode[1]}, phoneNumber: ${phone[1]}${phone[2]}`);
};

// Check parsePhoneNumber
console.log(parsePhoneNumber('206-333-4444'));
// returns {areaCode: '206', phoneNumber: '3334444'}
//returns {areaCode: '206', phoneNumber: '3334444'}

console.log(parsePhoneNumber('(222) 422-5353'));
// returns {areaCode: '222', phoneNumber: '4225353'}
18 changes: 17 additions & 1 deletion soccer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ const getPointsFromResult = function getPointsFromResult(result) {
// including wins, draws, and losses i.e. 'wwdlw'
// Returns total number of points won

const getTotalPoints = function(results) {
let separatedResults = results.split("");
let total = 0;

separatedResults.forEach((result) => {
const pointsForResult = getPointsFromResult(result);
total += pointsForResult;
});
return total;
};

// Check getTotalPoints
console.log(getTotalPoints('wwdl')); // should equal 7
Expand All @@ -30,13 +39,20 @@ console.log(getTotalPoints('wwdl')); // should equal 7
// i.e. {name: 'Sounders', results: 'wwlwdd'}
// Logs each entry to the console as "Team name: points"


const orderTeams = (...teamsInfo) => {
// why not teamsInfo.forEach(team) => { - is it the same?
teamsInfo.forEach(function (team) {
const pointsForTeam = getTotalPoints(team.results);
console.log(`${team.name}: ${pointsForTeam}`);
});
};

// Check orderTeams
orderTeams(
{ name: 'Sounders', results: 'wwdl' },
{ name: 'Galaxy', results: 'wlld' }
);

// should log the following to the console:
// Sounders: 7
// Galaxy: 4
16 changes: 16 additions & 0 deletions spaceShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@
// - 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;
}

accelerate() {
const {name,topSpeed} = this;
console.log(`${name} moving to ${topSpeed}`);
}
}

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

const discovery = new SpaceShip('Discovery', '360000mph');
discovery.accelerate();

const enterprise = new SpaceShip('Enterprise', '840000mph');
enterprise.accelerate();