Skip to content

Completed Homework excercises #12

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
17 changes: 16 additions & 1 deletion battleGame.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
// 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() * 10);
const damageToPlayer = defendingPlayer.health - totalDamage;
return `${attackingPlayer.name} hits ${defendingPlayer.name} for ${totalDamage} points`
}*/



// 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: 'Cloud', health: 10}
const player2 = {name: 'Sephiroth', 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() * 10);
defendingPlayer.health = defendingPlayer.health - totalDamage;
return `${attackingPlayer.name} hits ${defendingPlayer.name} for ${totalDamage} points`
}


// DO NOT MODIFY THE CODE BELOW THIS LINE
Expand All @@ -23,7 +37,8 @@ while (player1.health >= 1 && player2.health >= 1 && preventInfiniteLoop > 0) {
const [attackingPlayer, defendingPlayer] = attackOrder;
console.log(attack(attackingPlayer, defendingPlayer, 1, 2));
attackOrder = attackOrder.reverse();

console.log(player1.health);
console.log(player2.health);
preventInfiniteLoop--;
}
const eliminatedPlayer = player1.health <= 0 ? player1 : player2;
Expand Down
9 changes: 9 additions & 0 deletions itemizedReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
// i.e. {descr: 'Coke', price: 1.99}
// function should log each item to the console and log a total price

let logReceipt = function(...menuItems){

let totalCost = 0;
menuItems.forEach(function(menuItem){
totalCost += menuItem.price;
console.log(`${menuItem.descr} costs ${menuItem.price}`)
console.log(`Your running total is ${totalCost}`)
})
};


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

const globalRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/gm;

let testPhoneNumber = function(phoneString){
return globalRegex.test(phoneString);
};



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

function parsePhoneNumber(phoneString){
const newRegex = new RegExp(globalRegex);
const result = newRegex.exec(phoneString);
const areaCode = result[1];
const phoneNumber = `${result[2]}${result[3]}`;

const phoneNumberObject = {areaCode:`${areaCode}`, phoneNumber:`${phoneNumber}`};

return phoneNumberObject;
};



// Check parsePhoneNumber
Expand Down
36 changes: 33 additions & 3 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,7 +11,11 @@ const RESULT_VALUES = {
* @param {string} result
* @returns {number} point value
*/
const getPointsFromResult = function getPointsFromResult(result) {
/*const getPointsFromResult = function getPointsFromResult(result) {
return RESULT_VALUES[result];
}*/

function getPointsFromResult(result) {
return RESULT_VALUES[result];
}

Expand All @@ -21,6 +24,19 @@ const getPointsFromResult = function getPointsFromResult(result) {
// Returns total number of points won


let getTotalPoints = function(pointsString){
let pointsArray = Array.from(pointsString);

let sum = 0

pointsArray.forEach(function(result){

sum += getPointsFromResult(result);
})

return sum;

};

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

/*let orderTeams = function(...teams){
let teamsArray = ...teams;
console.log(teamsArray);


};*/

const orderTeams = (...teams) => {
teams.forEach(team => {
let teamResults = `${team.results}`
console.log(`${team.name}: ${getTotalPoints(teamResults)}`);
})
};



// Check orderTeams
Expand All @@ -39,4 +69,4 @@ orderTeams(
);
// should log the following to the console:
// Sounders: 7
// Galaxy: 4
// Galaxy: 4
26 changes: 26 additions & 0 deletions spaceShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,33 @@
// - 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( `The ${name} moving to ${topSpeed} !`)
}

failToLaunch(){
const{name} = this;
console.log(`The ${name} failed to launch!`)
}
}


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

let cowboyBebop = new SpaceShip('Bebop' , '500mph');
let cidsAirship = new SpaceShip ('Highwind' , '700 mph');
let noctisAirship = new SpaceShip ('Regalia' , '500 mph')
let blueOrigin = new SpaceShip('Blue Orgin', '350mph');

cowboyBebop.accelerate();
cidsAirship.accelerate();
noctisAirship.accelerate();
blueOrigin.failToLaunch();