Skip to content

Complete exercises #2

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


// const attack = function (attackingPlayer, defendingPlayer, baseDamage, variableDamage){
//
// let damage = baseDamage + getRandomIntInclusive(0,variableDamage)
// defendingPlayer.health-=damage
// return `${attackingPlayer.name}+"hits"+${defendingPlayer.name} +"for "+${damage}`
//
// }

function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min); //The maximum is inclusive and the minimum is inclusive
}

// 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: 'Merlin',
health: 10,

}

const 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 damage = baseDamage + getRandomIntInclusive(0,variableDamage)
defendingPlayer.health-=damage
return `${attackingPlayer.name}+"hits"+${defendingPlayer.name} +"for "+${damage}`

}

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

const logReceipt = function (){
let total=0
const args = Array.from(arguments)
args.forEach(function (thing){
console.log(thing.descr+" - "+ "$"+thing.price)
total+=thing.price


})
console.log(`Total - $${total}`)

}

// console.log(logReceipt(10,6,99))


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

const testPhoneNumber = function (phoneNumber){
// console.log(phoneNumber)
let phoneTest = phoneNumber.match(/^\(\d{3}\)[-\s]\d{3}[-\s]\d{4}$/)

if (phoneTest!=null){
return true
}
else{
return false
}

}

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

const parsePhoneNumber = function (parsePhone){
let areaMatch = /\(?\d{3}\)?/.exec(parsePhone)

let areaCode = areaMatch[0].replace(/\(/,'')

let area = areaCode.replace(/\)/,'')

let phone = /\d{3}-\d{4}/.exec(parsePhone)

phoneObject = {
areaCode:area,
phoneNumber:phone[0].replace('-','')
}
// return `areaCode: '${phoneObject.area}', phoneNumber: '${phoneObject.phone}'`
return phoneObject

}

// Check parsePhoneNumber
console.log(parsePhoneNumber('206-333-4444'));
Expand Down
88 changes: 56 additions & 32 deletions soccer.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,66 @@
(function (){

const RESULT_VALUES = {
w: 3,
d: 1,
l: 0
}
const RESULT_VALUES = {
w: 3,
d: 1,
l: 0
}

/**
* Takes a single result string and (one of 'w', 'l', or 'd')
* and returns the point value
*
* @param {string} result
* @returns {number} point value
*/
const getPointsFromResult = function getPointsFromResult(result) {
return RESULT_VALUES[result];
}
/**
* Takes a single result string and (one of 'w', 'l', or 'd')
* and returns the point value
*
* @param {string} result
* @returns {number} point value
*/
const getPointsFromResult = function getPointsFromResult(result) {
return RESULT_VALUES[result];
}

// Create getTotalPoints function which accepts a string of results
// including wins, draws, and losses i.e. 'wwdlw'
// Returns total number of points won
// Create getTotalPoints function which accepts a string of results
// including wins, draws, and losses i.e. 'wwdlw'
// Returns total number of points won

const getTotalPoints = function(results){

let points = 0

// Check getTotalPoints
console.log(getTotalPoints('wwdl')); // should equal 7
for (let char of results){

// create orderTeams function that accepts as many team objects as desired,
// 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"
points +=getPointsFromResult(char)

}

return points

// Check orderTeams
orderTeams(
{ name: 'Sounders', results: 'wwdl' },
{ name: 'Galaxy', results: 'wlld' }
);
// should log the following to the console:
// Sounders: 7
// Galaxy: 4
}

// Check getTotalPoints
console.log(getTotalPoints('wwdl')); // should equal 7

// create orderTeams function that accepts as many team objects as desired,
// 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"

// Check orderTeams

const orderTeams = function (){

const args = Array.from(arguments)
args.forEach(function (thing){
console.log(`${thing.name}: ${getTotalPoints(thing.results)}`)
})

}

orderTeams(
{ name: 'Sounders', results: 'wwdl' },
{ name: 'Galaxy', results: 'wlld' }
);

// should log the following to the console:
// Sounders: 7
// Galaxy: 4

})();
16 changes: 16 additions & 0 deletions spaceShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@
// - 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
}

acccelerate(){
const {name, topSpeed} = this
console.log(`${name} moving to ${topSpeed} mph`)
}
}

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

const falcon = new SpaceShip('falconOne', 1000)
falcon.acccelerate()

const bigBird = new SpaceShip('birdOne', 600)
bigBird.acccelerate()