Skip to content

Complete exercises #19

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
21 changes: 19 additions & 2 deletions battleGame.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
// 1. Create attack function below. This will take the following parameters:
// attackingPlayer, defendingPlayer, baseDamage, variableDamage
// function attack(attackingPlayer, defendingPlayer, baseDamage, variableDamage) {
// randomDamage = Math.floor(Math.random() * variableDamage);
// totalDamage = baseDamage + randomDamage;
// defendingPlayer.health -= totalDamage;

// console.log(
// `${attackingPlayer.name} hits ${defendingPlayer.name} for ${totalDamage} Damage.
// ${defendingPlayer.name} has ${defendingPlayer.health} health left!`);
// }


// 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: 'Scorpion', health: 10};
const player2 = {name: 'Johnny Cage', health: 10};


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

const attack = (attackingPlayer, defendingPlayer, baseDamage, variableDamage) => {
randomDamage = Math.floor(Math.random() * variableDamage);
totalDamage = baseDamage + randomDamage;
defendingPlayer.health -= totalDamage;

console.log(
`${attackingPlayer.name} hits ${defendingPlayer.name} for ${totalDamage} Damage.
${defendingPlayer.name} has ${defendingPlayer.health} health left!`);
}


// DO NOT MODIFY THE CODE BELOW THIS LINE
Expand Down
16 changes: 16 additions & 0 deletions itemizedReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@
// i.e. {descr: 'Coke', price: 1.99}
// function should log each item to the console and log a total price

function logReceipt(...receipt){
let sum = 0;
let taxSum = 0;

const taxRate = 0.104;

receipt.forEach(receipt => {
taxSum += taxRate * receipt.price;
sum += receipt.price;
console.log(`${receipt.descr} - $${receipt.price}`)
});

console.log
(`Subtotal : $${sum}
Taxes : $${taxSum.toFixed(2)}
Total : $${(sum + taxSum).toFixed(2)}`)
};

// Check
logReceipt(
Expand Down
29 changes: 28 additions & 1 deletion phoneNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@
// '206 333 4444'
// Returns true if valid, false if not valid

const phoneNumberStr = '(206) 333-4444';
// const phoneNumberStr = '206-333-4444';
// const phoneNumberStr = '206 333 4444';

function testPhoneNumber(phoneNumber) {
const checkParenthesis = /^\(\d{3}\)/.test(phoneNumber);

if (checkParenthesis == true) {
const regexPhone = /^\(\d{3}\)[-\s]\d{3}[-\s]\d{4}$/.test(phoneNumber);
return regexPhone;
} else {
const regexPhone = /^\d{3}[-\s]\d{3}[-\s]\d{4}$/.test(phoneNumber);
return regexPhone;
}
}

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


function parsePhoneNumber(phoneNumber) {
const checkParenthesis = /^\(\d{3}\)/.test(phoneNumber);
if (checkParenthesis == true) {
const regexAreaCode = /^\((\d{3})\)[-\s](\d{3})[-\s](\d{4})$/;
const capturePhoneNum = regexAreaCode.exec(phoneNumber);
const numberCaptured = {areaCode: capturePhoneNum[1], phoneNumber: capturePhoneNum[2] + capturePhoneNum[3]};
return numberCaptured;
} else {
const regexAreaCode = /^(\d{3})[-\s](\d{3})[-\s](\d{4})$/;
const capturePhoneNum = regexAreaCode.exec(phoneNumber);
const numberCaptured = {areaCode: capturePhoneNum[1], phoneNumber: capturePhoneNum[2] + capturePhoneNum[3]};
return numberCaptured;
}
}

// Check parsePhoneNumber
console.log(parsePhoneNumber('206-333-4444'));
Expand Down
27 changes: 21 additions & 6 deletions soccer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

(function () {
const RESULT_VALUES = {
w: 3,
d: 1,
l: 0
w: 3,
d: 1,
l: 0
}

/**
Expand All @@ -19,8 +20,15 @@ const getPointsFromResult = function getPointsFromResult(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(results){
let points = 0;
const strArray = results.split('');


strArray.forEach(strArray => {
points += getPointsFromResult(strArray);
});
return points;
}

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


function orderTeams (...teamObjects) {
teamObjects.forEach(teamObjects => {
let points = getTotalPoints(teamObjects.results);
console.log(`${teamObjects.name}: ${points}`)
});
}

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

})();
// should log the following to the console:
// Sounders: 7
// Galaxy: 4
18 changes: 18 additions & 0 deletions spaceShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@
// - 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 falcon = new SpaceShip('Falcon', 'lightspeed');
falcon.accelerate();

const winnebago = new SpaceShip('1986 Winnebago Chieftain 33', 'Ludicrous Speed');
winnebago.accelerate();

const rover = new SpaceShip('Mars Rover', 19000);
rover.accelerate();