Skip to content

Yuhong solution to week 3 homework. #9

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
31 changes: 21 additions & 10 deletions battleGame.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
// 1. Create attack function below. This will take the following parameters:
// attackingPlayer, defendingPlayer, baseDamage, variableDamage
// 1. Create attackFunc function below. This will take the following parameters:
// attackFuncingPlayer, defendingPlayer, baseDamage, variableDamage

// function attackFunc(attackFuncingPlayer, defendingPlayer, baseDamage, variableDamage) {
// let damage = Math.round(baseDamage + Math.random() * variableDamage);
// defendingPlayer.health = defendingPlayer.health - damage;
// return `${attackFuncingPlayer.name} hits ${defendingPlayer.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
let player1 = {name: "Jack", health: 10}
let player2 = {name: "Mike", health: 10}


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

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


const attackFunc = (attackFuncingPlayer, defendingPlayer, baseDamage, variableDamage) => {
let damage = Math.round(baseDamage + Math.random() * variableDamage);
defendingPlayer.health = defendingPlayer.health - damage;
return `${attackFuncingPlayer.name} hits ${defendingPlayer.name} for ${defendingPlayer.health} damage`;
}

// DO NOT MODIFY THE CODE BELOW THIS LINE
// Set attacker and defender. Reverse roles each iteration
let attackOrder = [player1, player2];
// Set attackFuncer and defender. Reverse roles each iteration
let attackFuncOrder = [player1, player2];

// Everything related to preventInfiniteLoop would not generally be necessary, just adding to
// safeguard students from accidentally creating an infinite loop & crashing browser
let preventInfiniteLoop = 100;
while (player1.health >= 1 && player2.health >= 1 && preventInfiniteLoop > 0) {
const [attackingPlayer, defendingPlayer] = attackOrder;
console.log(attack(attackingPlayer, defendingPlayer, 1, 2));
attackOrder = attackOrder.reverse();
const [attackFuncingPlayer, defendingPlayer] = attackFuncOrder;
console.log(attackFunc(attackFuncingPlayer, defendingPlayer, 1, 2));
attackFuncOrder = attackFuncOrder.reverse();

preventInfiniteLoop--;
}
Expand Down
10 changes: 10 additions & 0 deletions itemizedReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
// i.e. {descr: 'Coke', price: 1.99}
// function should log each item to the console and log a total price

let total = 0;
const logReceipt = function() {
const args = Array.from(arguments);
args.forEach(function(receipt) {
total += receipt.price;
console.log(`${receipt.descr} - ${receipt.price}`);
console.log(`Running total: ${total}`);
});
console.log(`Total - ${total}`);
}


// Check
Expand Down
21 changes: 19 additions & 2 deletions phoneNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
// '206-333-4444'
// '206 333 4444'
// Returns true if valid, false if not valid
function testPhoneNumber(phoneNbr) {
let regex1 = /^\(?([0-9]{3})\)?[- ]?([0-9]{3})[- ]?([0-9]{4})$/
return regex1.test(phoneNbr); //result is: true
}



Expand All @@ -29,8 +33,21 @@ 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(phoneNbr) {
let regexMix = /([0-9]{4}|[0-9]{3})/g;
let arrayExtraction;
let nbr = "";
let areaCode = "";
let i = 0;
while ((arrayExtraction = regexMix.exec(phoneNbr)) !== null) {
if (i == 0)
areaCode = arrayExtraction[0];
else
nbr += arrayExtraction[0];
i++;
}
return `areaCode: '${areaCode}', phoneNumber: '${nbr}'`;
}

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

(function(){
const RESULT_VALUES = {
w: 3,
d: 1,
Expand All @@ -19,7 +19,13 @@ 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 (resultStr) {
let totalPts = 0;
[...resultStr].forEach(element => {
totalPts += getPointsFromResult(element)
});
return totalPts;
}


// Check getTotalPoints
Expand All @@ -29,7 +35,12 @@ console.log(getTotalPoints('wwdl')); // should equal 7
// 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"

function orderTeams() {
const args = Array.from(arguments);
args.forEach(function(team){
console.log(`${team.name}: ${getTotalPoints(team.results)}`);
});
}


// Check orderTeams
Expand All @@ -39,4 +50,5 @@ orderTeams(
);
// should log the following to the console:
// Sounders: 7
// Galaxy: 4
// Galaxy: 4
})();
18 changes: 17 additions & 1 deletion spaceShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@
// - 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(name, topSpeed){
console.log(`${this.name} moving to ${this.topSpeed}`);
}
}
const ss1 = new Spaceship("Voyager", "130kmps");
const ss2 = new Spaceship("Expedition", "160kmps");


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

//Here is the result for above two method calls:
//Voyager moving to 130kmps
//spaceShip.js:11 Expedition moving to 160kmps