Skip to content

Week 3 Assignment #21

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 5 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
26 changes: 22 additions & 4 deletions battleGame.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
// 1. Create attack function below. This will take the following parameters:
// attackingPlayer, defendingPlayer, baseDamage, variableDamage


// let attack = function(attackingPlayer,defendingPlayer,baseDamage,variableDamage){
// const randomDamage = Math.floor(Math.random()*variableDamage);
// const totalDamage = baseDamage + randomDamage;
// defendingPlayer.health -= totalDamage;
// return `${attackingPlayer.name} attacked ${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


// const player1 = {
// name = 'Matt',
// health = 10
// };
// const player2 = {
// name = 'James',
// health = 10
// };

// 3. Refactor attack function to an arrow function. Comment out function above.
let attack = (attackingPlayer,defendingPlayer,baseDamage,variableDamage) =>{
const randomDamage = Math.floor(Math.random()*variableDamage);
const totalDamage = baseDamage + randomDamage;
defendingPlayer.health -= totalDamage;


return `${attackingPlayer.name} attacked ${defendingPlayer.name} for ${totalDamage}`;
}
const player1 = {name :'Matt',health : 10};
const player2 = {name : 'James',health : 10};

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

const logReceipt = function(...menuItems){
let total = 0;
menuItems.forEach(function(menuItem) {
total += menuItem.price;
console.log(`${menuItem.descr} sells for ${menuItem.price}.`);
console.log(`Running Total = ${total}`);
})
}


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


const testPhoneNumber = (phoneNumber) => {
var phoneno = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
return phoneno.test(phoneNumber);
}
testPhoneNumber('2063334444');

// Explanation of RegExp
// ^ start of line
Expand All @@ -22,19 +25,24 @@
console.log(testPhoneNumber('(206) 333-4444')); // should return true
console.log(testPhoneNumber('(206) 33-4444')); // should return false, missing a digit


// 1. Create a function parsePhoneNumber that takes in a phoneNumber string
// in one of the above formats. For this, you can *assume the phone number
// passed in is correct*. This should use a regular expression
// 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}
const parsePhoneNumber = (phoneNumber) => {
const phoneNumberParts = /^\(?(\d{3})[\)\s-]+(\d{3})[\s-](\d{4})$/.exec(phoneNumber);
const [, areaCode, first, last] = phoneNumberParts;

const phoneNo = {areaCode,phoneNumber:`${first}${last}`};

return phoneNo;
}

// 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'}
16 changes: 13 additions & 3 deletions soccer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ const RESULT_VALUES = {
* @param {string} result
* @returns {number} point value
*/
const getPointsFromResult = function getPointsFromResult(result) {
return RESULT_VALUES[result];
const getTotalPoints = function getPointsFromResult(result) {
//return RESULT_VALUES[result];
var output = 0;
[...result].forEach((character)=>{
output += RESULT_VALUES[character];
});

return output;
}

// Create getTotalPoints function which accepts a string of results
Expand All @@ -29,7 +35,11 @@ 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"

const orderTeams = (...teamsArr) =>{
[...teamsArr].forEach((team) => {
console.log(team.name+':'+ getTotalPoints(team.results)+ '\n');
});
}


// Check 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(){
const{name,topSpeed} = this;
console.log(`${name} moving to speed ${topSpeed}`);
};
};


// 2. Call the constructor with a couple ships,
// and call accelerate on both of them.
const call = new SpaceShip('Iron Man',90);
call.accelerate();

const say = new SpaceShip('Hulk',150);
say.accelerate();