Skip to content

Complete exercises #20

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 2 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Week-3
21 changes: 21 additions & 0 deletions battleGame.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
// 1. Create attack function below. This will take the following parameters:
// attackingPlayer, defendingPlayer, baseDamage, variableDamage

/* const attack = function(attackingPlayer, defendingPlayer, baseDamage, variableDamage){
randomNum = Math.floor(Math.random() * variableDamage) + 1;
const totalDamage = baseDamage + randomNum;
defendingPlayer.health -= totalDamage;
return 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
const player1 = {
name: 'He-Man',
health: 10
};

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


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

const attack = (attackingPlayer, defendingPlayer, baseDamage, variableDamage) => {
const randomNum = Math.floor(Math.random() * variableDamage) + 1;
const totalDamage = baseDamage + randomNum;
defendingPlayer.health -= totalDamage;
return `${attackingPlayer.name} hits ${defendingPlayer.name} for ${totalDamage} damage!`;
};


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


const priceTotal =() => {

}

const logReceipt = (...receipt) => {
let subTotal = 0;
receipt.forEach(receipt => {
console.log(`${receipt.descr} - ${receipt.price}`)
subTotal += receipt.price;
})
let taxes = subTotal * 0.10;
let total = taxes + subTotal;
console.log(`Subtotal = ${subTotal}\nTaxes = ${taxes.toFixed(2)}\nTotal = ${total.toFixed(2)}`)
};

//print out array



// Check
logReceipt(
Expand All @@ -16,3 +34,6 @@ logReceipt(
// Chips & Salsa - $2.99
// Sprite - $1.99
// Total - $10.97


// A(n) 'this' allows you to call the function above where the function is defined.
25 changes: 22 additions & 3 deletions phoneNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@
// '206 333 4444'
// Returns true if valid, false if not valid



const formatOne = /^(\(\d{3}\))(\s)(\d{3})(-)(\d{4})/;
const formatTwo = /^(\d{3})(-)(\d{3})(-)(\d{4})/;
const formatThree = /^(\d{3})(\s)(\d{3})(\s)(\d{4})/;

const testPhoneNumber =(number) => {

if (formatOne.test(number) || formatTwo.test(number) || formatThree.test(number)){
console.log(`Test Phone Number: ${number} is`,true);
} else {
console.log(`Test Phone Number: ${number} is`,false);
}
};

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

const parsePhoneNumber =(number) => {
let aCode= /\d{3}/;
let pNum = /.{9}$/;
let areaCode = aCode.exec(number);
let rawNumber = pNum.exec(number);
let phoneNumber = String(rawNumber).replace(/-|\s/g,'');

return `areaCode: '${areaCode}', phoneNumber: '${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'}
// returns {areaCode: '222', phoneNumber: '4225353'}
26 changes: 25 additions & 1 deletion soccer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check


const RESULT_VALUES = {
w: 3,
Expand All @@ -12,6 +14,9 @@ const RESULT_VALUES = {
* @param {string} result
* @returns {number} point value
*/



const getPointsFromResult = function getPointsFromResult(result) {
return RESULT_VALUES[result];
}
Expand All @@ -20,6 +25,23 @@ const getPointsFromResult = function getPointsFromResult(result) {
// including wins, draws, and losses i.e. 'wwdlw'
// Returns total number of points won

// spent too much time trying to figure out the soccer exercise on my own
// i ended up doing the extra credit for the receipt exercise, going to focus on week 4 assignment.
const getTotalPoints = (strPoints) => {
// code block here
let totalPoints = 0;

// const pointValues = new RESULT_VALUES; // not sure how to tie in the point values into the strPoints below

let sepPoints = strPoints.split(',');

sepPoints.forEach(function(number){
totalPoints += number;
});

return totalPoints;
};



// Check getTotalPoints
Expand All @@ -30,7 +52,9 @@ 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 = () => {
// code block here
};

// 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 @@ -2,8 +2,22 @@
// - should set two properties: name and topSpeed
// - 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.

let Enterprise = new SpaceShip('USS Enterprise NCC-1701-D','Warp 9.5');
let Voyager = new SpaceShip('USS Voyager NCC-74656','Warp 9.975');

Enterprise.accelerate();
Voyager.accelerate();