Skip to content

Khanh's week 3 assignment complete #11

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
28 changes: 25 additions & 3 deletions battleGame.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
/**

// 1. Create attack function below. This will take the following parameters:
// attackingPlayer, defendingPlayer, baseDamage, variableDamage
const attack = function(attackingPlayer, defendingPlayer, baseDamage, variableDamage) {
//generate a random number from the range of 0 to the value of the variableDamage
let randomInt = Math.floor(Math.random() * (variableDamage - 0)) + 0;
// calculate totalDamage which is the sum of baseDamage and ranDomInt;
// reduce the health property of the defending player by the amount of the totalDamage
let totalDamage = baseDamage + randomInt;
defendingPlayer.health -= totalDamage;
// return the string describe the attack
return `${attackingPlayer.name} hits ${defendingPlayer.name} for ${totalDamage}`;


}
**/

// 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: "Tommy The Great", health: 10};
let player2 = {name: "Master Sunny", health: 10};

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

const attack = (attackingPlayer, defendingPlayer, baseDamage, variableDamage) => {
//generate a random number from the range of 0 to the value of the variableDamage
let randomInt = Math.floor(Math.random() * (variableDamage - 0)) + 0;
// calculate totalDamage which is the sum of baseDamage and ranDomInt;
// reduce the health property of the defending player by the amount of the totalDamage
let totalDamage = baseDamage + randomInt;
defendingPlayer.health -= totalDamage;
// return the string describe the attack
return `${attackingPlayer.name} hits ${defendingPlayer.name} for ${totalDamage}`;

}

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

const logReceipt = (...args) =>
{
let subtotal = 0;
let taxrate = 0.103
args.forEach(obj =>
{
subtotal += obj.price;
console.log (`${obj.descr} - $${obj.price} - Sub total: $${subtotal}`);
})
// calculate total after tax
total = subtotal + (subtotal * taxrate);
console.log(`Total after tax - $${total.toFixed(2)}`);
}


// Check
Expand Down
27 changes: 24 additions & 3 deletions phoneNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
// '206 333 4444'
// Returns true if valid, false if not valid


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

// Explanation of RegExp
// ^ start of line
Expand All @@ -29,8 +34,24 @@ console.log(testPhoneNumber('(206) 33-4444')); // should return false, missing a
// 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}


function parsePhoneNumber(strPhone)
{
//define phone object
phoneObj = {areaCode: null, phoneNumber: null};
//match phone number patter
const ac = new RegExp(/^\(?(\d{3})\)?[-\s](\d{3})[-\s](\d{4})$/);
// run exec method to capture phone number and remaining part of phone number
// by removing non numeric character globally and
// return only digits
ph = ac.exec(strPhone)[0].replace(/\D/g,'');
// set areaCode to the first 3 numeric character
phoneObj.areaCode = ph.substring(0,3);
// set phoneNumber to the last 7 numeric character
phoneObj.phoneNumber = ph.substring(3,ph.length);
// return the phone object
return phoneObj;

}

// Check parsePhoneNumber
console.log(parsePhoneNumber('206-333-4444'));
Expand Down
43 changes: 22 additions & 21 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 @@ -19,24 +18,26 @@ 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



const getTotalPoints = function getTotalPoints(strResult) {
let totalPoint = 0;
//loop through each char in string,
// calculate running total point by calling getPointsFromResult function
for (let c in strResult) {
totalPoint += getPointsFromResult(strResult[c]);
}
return totalPoint;
}
// Check getTotalPoints
console.log(getTotalPoints('wwdl')); // should equal 7

// 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"



// Check orderTeams
orderTeams(
{ name: 'Sounders', results: 'wwdl' },
{ name: 'Galaxy', results: 'wlld' }
);
// should log the following to the console:
// Sounders: 7
// Galaxy: 4
console.log(getTotalPoints("wwdl"));
//console.log(getTotalPoints('wwdl')); //should equal 7
//Complete orderResults function.
//This accepts unlimited team objects { name, results },
//and logs the team name & points
const orderResults = (...args) => {
const ar = Array.from(args)
ar.forEach(obj => {
console.log(`${obj.name} scores ${obj.point}`);
});
};

orderResults({name:'Titan', point: 12},{name: 'Saturn', point: 30},{name:'Galaxy', point: 24});
20 changes: 20 additions & 0 deletions spaceShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,28 @@
// - 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()
{
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 mySS1 = new SpaceShip("Hercules",80);
const mySS2 = new SpaceShip("Challenger", 40);
const mySS3 = new SpaceShip("Saturn", 60);
mySS1.accelerate();
mySS2.accelerate();
mySS3.accelerate();