Skip to content

week3_completed #6

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
23 changes: 22 additions & 1 deletion 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){
let total = 0;
total = baseDamage+Math.ceil(Math.random()*variableDamage);
defendingPlayer.health-=total;
return `${player1.name} hits ${player2.name} by ${total} 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:"Merlin",
health: 10
};
let player2 = {
name:"James Bond",
health: 10
};


// 3. Refactor attack function to an arrow function. Comment out function above.
const attack = (attackingPlayer,defendingPlayer,baseDamage,variableDamage)=>{
let total = 0;
total = baseDamage+Math.ceil(Math.random()*variableDamage);
defendingPlayer.health -=total;
return `${player1.name} hits ${player2.name} by ${total} damage.`

}


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


const logReceipt = (...args)=>{
let total = 0;
args.forEach((item)=>{
console.log(`${item.descr} - $${item.price}`);
total+=item.price;
})
console.log(`Total - $${total}`);
}

// Check
logReceipt(
Expand Down
22 changes: 20 additions & 2 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 = (phoneNumber)=>{

let regex = new RegExp(/^\(?\d{3}\)?[-\s]\d{3}[-\s]\d{4}$/);
let result = regex.test(phoneNumber);
return result;
}

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


const parsePhoneNumber = (phoneNumber)=>{
let regex = new RegExp(/^\(?([0-9]{3})\)?[-\s]([0-9]{3})[-\s]([0-9]{4})$/);
let object ={};
if(testPhoneNumber(phoneNumber)){
let areaCode = regex.exec(phoneNumber);
if(areaCode !== null){
object = {
areaCode: areaCode[1],
phoneNumber: areaCode[2]+areaCode[3]
}
}
}
return object;
}

// Check parsePhoneNumber
console.log(parsePhoneNumber('206-333-4444'));
Expand Down
28 changes: 26 additions & 2 deletions soccer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ const getPointsFromResult = function getPointsFromResult(result) {
// including wins, draws, and losses i.e. 'wwdlw'
// Returns total number of points won

function getTotalPoints(results){
let word = results.split('');
let total = 0;
word.forEach((letter)=>{
if(RESULT_VALUES[letter]){
total +=RESULT_VALUES[letter];
}
})
return total;
}



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


function orderTeams(...args){
args.forEach((arg)=>{
let word = arg.results.split('');
let total = 0;
word.forEach((letter)=>{
if(RESULT_VALUES[letter]){
total +=RESULT_VALUES[letter];
}
})
console.log(`${arg.name}: ${total}`);
})
}

// Check orderTeams
orderTeams(
{ name: 'Sounders', results: 'wwdl' },
{ name: 'Galaxy', results: 'wlld' }
{ name: 'Galaxy', results: 'wlld' },
{ name: 'Stars', results: 'wlldd' },
{ name: 'Moon', results: 'wlldw' }
);
// should log the following to the console:
// Sounders: 7
Expand Down
13 changes: 13 additions & 0 deletions spaceShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@
// - 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}m/s`)
}

}

// 2. Call the constructor with a couple ships,
// and call accelerate on both of them.
const ship = new SpaceShip("Rocket", 100000);
ship.accelerate();