Skip to content

finished week 3 homework #13

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
25 changes: 21 additions & 4 deletions battleGame.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@

// 1. Create attack function below. This will take the following parameters:
// attackingPlayer, defendingPlayer, baseDamage, variableDamage


// const attack = function (attackingPlayer, defendingPlayer, baseDamage, variableDamage) {
// const randomDamage = Math.floor(Math.random() * variableDamage);
// const totalDamage = baseDamage + randomDamage;
// defendingPlayer.health = defendingPlayer.health - totalDamage;
// console.log(`${attackingPlayer.name} hits ${defendingPlayer.name} for ${totalDamage} 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: 'Corgi',
health: 10
};
let player2 = {
name: 'Bichon',
health: 10
};


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


const attack = (attackingPlayer, defendingPlayer, baseDamage, variableDamage) => {
const randomDamage = Math.floor(Math.random() * variableDamage);
const totalDamage = baseDamage + randomDamage;
defendingPlayer.health = defendingPlayer.health - totalDamage;
console.log(`${attackingPlayer.name} hits ${defendingPlayer.name} for ${totalDamage} damage`);
}

// DO NOT MODIFY THE CODE BELOW THIS LINE
// Set attacker and defender. Reverse roles each iteration
Expand Down
11 changes: 11 additions & 0 deletions itemizedReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@
// 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(menuItem => console.log(`descr: ${menuItem.descr}, price: $${menuItem.price}`))
menuItems.forEach(menuItem => total += menuItem.price);
console.log(`Total - $${total}`)

// return receipt
}

// const array1 = ['a', 'b', 'c'];

// array1.forEach(element => console.log(element));

// Check
logReceipt(
Expand Down
88 changes: 54 additions & 34 deletions phoneNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,57 @@
// '206-333-4444'
// '206 333 4444'
// Returns true if valid, false if not valid



// Explanation of RegExp
// ^ start of line
// \( optional start parenthesis
// \d{3} exactly 3 digit characters
// \) optional end parenthesis
// [-\s] one of a space or a dash
// \d{3} exactly 3 digit characters
// [-\s] one of a space or a dash
// \d{4} exactly 4 digit characters
// $ end of line

// check testPhoneNumber
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}



// Check parsePhoneNumber
console.log(parsePhoneNumber('206-333-4444'));
// returns {areaCode: '206', phoneNumber: '3334444'}

console.log(parsePhoneNumber('(222) 422-5353'));
// returns {areaCode: '222', phoneNumber: '4225353'}
function testPhoneNumber(phoneNumber) {
const regEx = /^\(\d{3}\)[-\s]\d{3}[-\s]\d{4}$/;
if (phoneNumber.match(regEx)) {
return true;
} else {
alert("message");
return false;
}

};


// Explanation of RegExp
// ^ start of line
// \( optional start parenthesis
// \d{3} exactly 3 digit characters
// \) optional end parenthesis
// [-\s] one of a space or a dash
// \d{3} exactly 3 digit characters
// [-\s] one of a space or a dash
// \d{4} exactly 4 digit characters
// $ end of line

// check testPhoneNumber
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}
function parsePhoneNumber(phoneNumber) {
const regEx2 = /\d{3}/;
const regEx3 = /\d{3}[-\s]\d{4}/;
let areaCode = regEx2.exec(phoneNumber);
let number = regEx3.exec(phoneNumber);
let result = number[0].replace('-', '');
let finalNumber = `areaCode: '${areaCode}', phoneNumber: '${result}'`;
// console.log(areaCode);
// console.log(number);
// console.log(result);
return finalNumber;
}


// Check parsePhoneNumber
console.log(parsePhoneNumber('206-333-4444'));
// returns {areaCode: '206', phoneNumber: '3334444'}

console.log(parsePhoneNumber('(222) 422-5353'));
// returns {areaCode: '222', phoneNumber: '4225353'}
30 changes: 25 additions & 5 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 @@ -20,6 +19,14 @@ const getPointsFromResult = function getPointsFromResult(result) {
// including wins, draws, and losses i.e. 'wwdlw'
// Returns total number of points won

function getTotalPoints(points) {
const myArray = points.split('');
let total = 0;
myArray.forEach((arrayItem) => total += RESULT_VALUES[arrayItem]);

return total
}



// Check getTotalPoints
Expand All @@ -30,13 +37,26 @@ 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(...points) {
points.forEach((point) => {
const myArray = point.results.split('');
let total = 0;
myArray.forEach((arrayItem) => total += RESULT_VALUES[arrayItem]);

console.log(`name: ${point.name}, results: ${total}`)
})

};


// Check orderTeams
orderTeams(
{ name: 'Sounders', results: 'wwdl' },
{ name: 'Galaxy', results: 'wlld' }
);
orderTeams({
name: 'Sounders',
results: 'wwdl'
}, {
name: 'Galaxy',
results: 'wlld'
});
// should log the following to the console:
// Sounders: 7
// Galaxy: 4
18 changes: 16 additions & 2 deletions spaceShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@
// - 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 spaceship1 = new SpaceShip('UFO', '100 mph');
spaceship1.accelerate();

const spaceship2 = new SpaceShip('Gort', '10,000 mph');
spaceship2.accelerate();