Skip to content

Commit d7dd47c

Browse files
committed
Added responses to all excercises
1 parent f3f3d91 commit d7dd47c

File tree

5 files changed

+88
-8
lines changed

5 files changed

+88
-8
lines changed

battleGame.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
// 1. Create attack function below. This will take the following parameters:
22
// attackingPlayer, defendingPlayer, baseDamage, variableDamage
3+
4+
/*const attack = function (attackingPlayer, defendingPlayer, baseDamage, variableDamage){
5+
const totalDamage = baseDamage + Math.floor(Math.random() * 10);
6+
const damageToPlayer = defendingPlayer.health - totalDamage;
7+
return `${attackingPlayer.name} hits ${defendingPlayer.name} for ${totalDamage} points`
8+
}*/
39

410

511

612
// 2. Create player1 and player2 objects below
713
// Each should have a name property of your choosing, and health property equal to 10
814

15+
const player1 = {name: 'Cloud', health: 10}
16+
const player2 = {name: 'Sephiroth', health: 10}
17+
918

1019

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

22+
const attack = (attackingPlayer, defendingPlayer, baseDamage, variableDamage)=>{
23+
const totalDamage = baseDamage + Math.floor(Math.random() * 10);
24+
defendingPlayer.health = defendingPlayer.health - totalDamage;
25+
return `${attackingPlayer.name} hits ${defendingPlayer.name} for ${totalDamage} points`
26+
}
1327

1428

1529
// DO NOT MODIFY THE CODE BELOW THIS LINE
@@ -23,7 +37,8 @@ while (player1.health >= 1 && player2.health >= 1 && preventInfiniteLoop > 0) {
2337
const [attackingPlayer, defendingPlayer] = attackOrder;
2438
console.log(attack(attackingPlayer, defendingPlayer, 1, 2));
2539
attackOrder = attackOrder.reverse();
26-
40+
console.log(player1.health);
41+
console.log(player2.health);
2742
preventInfiniteLoop--;
2843
}
2944
const eliminatedPlayer = player1.health <= 0 ? player1 : player2;

itemizedReceipt.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
// i.e. {descr: 'Coke', price: 1.99}
44
// function should log each item to the console and log a total price
55

6+
let logReceipt = function(...menuItems){
7+
8+
let totalCost = 0;
9+
menuItems.forEach(function(menuItem){
10+
totalCost += menuItem.price;
11+
console.log(`${menuItem.descr} costs ${menuItem.price}`)
12+
console.log(`Your running total is ${totalCost}`)
13+
})
14+
};
615

716

817
// Check

phoneNumber.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
const globalRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/gm;
99

1010
let testPhoneNumber = function(phoneString){
11-
console.log(globalRegex.test(phoneString));
11+
return globalRegex.test(phoneString);
1212
};
1313

1414

@@ -37,14 +37,14 @@ console.log(testPhoneNumber('(206) 33-4444')); // should return false, missing a
3737
// Returns an object in the format {areaCode, phoneNumber}
3838

3939
function parsePhoneNumber(phoneString){
40-
const result = globalRegex.exec(phoneString);
41-
console.log(result);
40+
const newRegex = new RegExp(globalRegex);
41+
const result = newRegex.exec(phoneString);
4242
const areaCode = result[1];
4343
const phoneNumber = `${result[2]}${result[3]}`;
4444

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

47-
console.log(phoneNumberObject);
47+
return phoneNumberObject;
4848
};
4949

5050

soccer.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
const RESULT_VALUES = {
32
w: 3,
43
d: 1,
@@ -12,7 +11,11 @@ const RESULT_VALUES = {
1211
* @param {string} result
1312
* @returns {number} point value
1413
*/
15-
const getPointsFromResult = function getPointsFromResult(result) {
14+
/*const getPointsFromResult = function getPointsFromResult(result) {
15+
return RESULT_VALUES[result];
16+
}*/
17+
18+
function getPointsFromResult(result) {
1619
return RESULT_VALUES[result];
1720
}
1821

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

2326

27+
let getTotalPoints = function(pointsString){
28+
let pointsArray = Array.from(pointsString);
29+
30+
let sum = 0
31+
32+
pointsArray.forEach(function(result){
33+
34+
sum += getPointsFromResult(result);
35+
})
36+
37+
return sum;
38+
39+
};
2440

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

49+
/*let orderTeams = function(...teams){
50+
let teamsArray = ...teams;
51+
console.log(teamsArray);
52+
53+
54+
};*/
55+
56+
const orderTeams = (...teams) => {
57+
teams.forEach(team => {
58+
let teamResults = `${team.results}`
59+
console.log(`${team.name}: ${getTotalPoints(teamResults)}`);
60+
})
61+
};
62+
3363

3464

3565
// Check orderTeams
@@ -39,4 +69,4 @@ orderTeams(
3969
);
4070
// should log the following to the console:
4171
// Sounders: 7
42-
// Galaxy: 4
72+
// Galaxy: 4

spaceShip.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,33 @@
33
// - should have a method accelerate that logs to the console
44
// `${name} moving to ${topSpeed}`
55

6+
class SpaceShip {
7+
constructor(name, topSpeed){
8+
this.name = name;
9+
this.topSpeed = topSpeed;
10+
}
11+
12+
accelerate(){
13+
const{name, topSpeed} = this;
14+
console.log( `The ${name} moving to ${topSpeed} !`)
15+
}
16+
17+
failToLaunch(){
18+
const{name} = this;
19+
console.log(`The ${name} failed to launch!`)
20+
}
21+
}
622

723

824
// 2. Call the constructor with a couple ships,
925
// and call accelerate on both of them.
26+
27+
let cowboyBebop = new SpaceShip('Bebop' , '500mph');
28+
let cidsAirship = new SpaceShip ('Highwind' , '700 mph');
29+
let noctisAirship = new SpaceShip ('Regalia' , '500 mph')
30+
let blueOrigin = new SpaceShip('Blue Orgin', '350mph');
31+
32+
cowboyBebop.accelerate();
33+
cidsAirship.accelerate();
34+
noctisAirship.accelerate();
35+
blueOrigin.failToLaunch();

0 commit comments

Comments
 (0)