Skip to content

Homework 3 complete #16

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

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

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

// return `${attackingPlayer.name} hit ${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

const player1 = {
name: 'Tom',
health: 10,
};

const player2 = {
name: 'Bob',
health: 10,
};

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

const attack = (attackingPlayer, defendingPlayer, baseDamage, variableDamage) => {

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

return `${attackingPlayer.name} hit ${defendingPlayer.name} for ${totalDamage} damage`;
}

// DO NOT MODIFY THE CODE BELOW THIS LINE
// Set attacker and defender. Reverse roles each iteration
Expand All @@ -27,4 +47,4 @@ while (player1.health >= 1 && player2.health >= 1 && preventInfiniteLoop > 0) {
preventInfiniteLoop--;
}
const eliminatedPlayer = player1.health <= 0 ? player1 : player2;
console.log(`${eliminatedPlayer.name} has been eliminated!`);
console.log(`${eliminatedPlayer.name} has been eliminated!`);
8 changes: 7 additions & 1 deletion itemizedReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
// i.e. {descr: 'Coke', price: 1.99}
// function should log each item to the console and log a total price


const logReceipt = (...orders) => {
orders.forEach(order => {
console.log(`${order.descr} - $${order.price}`)
})
}

// Check
logReceipt(
{ descr: 'Burrito', price: 5.99 },
{ descr: 'Chips & Salsa', price: 2.99 },
{ descr: 'Sprite', price: 1.99 }
);

// should log something like:
// Burrito - $5.99
// Chips & Salsa - $2.99
// Sprite - $1.99
// Total - $10.97

76 changes: 43 additions & 33 deletions phoneNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,46 @@
// '206 333 4444'
// Returns true if valid, false if not valid



// Explanation of RegExp
// ^ start of line
// \( optional start parenthesis
// \d{3} exactly 3 digit characters
// \) optional end parenthesis
// [-\s] one of a space or a dash
// \d{3} exactly 3 digit characters
// [-\s] one of a space or a dash
// \d{4} exactly 4 digit characters
// $ end of line

// check testPhoneNumber
console.log(testPhoneNumber('(206) 333-4444')); // should return true
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
// 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}



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

console.log(parsePhoneNumber('(222) 422-5353'));
// returns {areaCode: '222', phoneNumber: '4225353'}
const testPhoneNumber = (num) => {
const validPhoneRegex = /[(]?\d{3}[)]?\s?-?\s?\d{3}\s?-?\s?\d{4}/g;
return validPhoneRegex.test(num);
}

// Explanation of RegExp
// ^ start of line
// \( optional start parenthesis
// \d{3} exactly 3 digit characters
// \) optional end parenthesis
// [-\s] one of a space or a dash
// \d{3} exactly 3 digit characters
// [-\s] one of a space or a dash
// \d{4} exactly 4 digit characters
// $ end of line

// check testPhoneNumber
console.log(testPhoneNumber('(206) 333-4444')); // should return true
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
// 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}

const parsePhoneNumber = (num) => {
const areaRegex = /\d{3}/;
const threeRegex = /[-\s]\d{3}/;
const fourRegex = /\d{4}/;
const area = areaRegex.exec(num)[0];
const three = threeRegex.exec(num)[0];
const four = fourRegex.exec(num)[0];
return { areaCode: area, phoneNumber: (`${three}${four}`).replace(/[-\s]/g, '') };
}

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

console.log(parsePhoneNumber('(222) 422-5353'));
// returns {areaCode: '222', phoneNumber: '4225353'}
23 changes: 19 additions & 4 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 @@ -12,15 +11,27 @@ const RESULT_VALUES = {
* @param {string} result
* @returns {number} point value
*/
const getPointsFromResult = function getPointsFromResult(result) {
const getPointsFromResult = function(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


const getTotalPoints = (record) => {
let score = 0;
record.split('').forEach(game => {
if (game === 'w') {
score += getPointsFromResult('w')
} else if (game === 'd') {
score += getPointsFromResult('d')
} else if (game === 'l') {
score += getPointsFromResult('l')
}
});
return score;
}

// Check getTotalPoints
console.log(getTotalPoints('wwdl')); // should equal 7
Expand All @@ -30,7 +41,11 @@ 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 = (...teams) => {
teams.forEach(team => {
console.log(`${team.name}: ${getTotalPoints(team.results)}`)
})
};

// Check orderTeams
orderTeams(
Expand Down
16 changes: 15 additions & 1 deletion spaceShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@
// - 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() {
console.log(`${this.name} moving to ${this.topSpeed}`);
}
};

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

const ship1 = new SpaceShip('Apollo 11', 100);
const ship2 = new SpaceShip('Apollo 13', 150);
ship1.accelerate();
ship2.accelerate();