Skip to content

Completed HW3 #17

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

function calculateVariableDamage(variableDamage)
{
return Math.round(Math.random() * variableDamage)
}
function createAttackMessage(attackingPlayerName, defendingPlayerName, damage)
{
return `${attackingPlayerName} hits ${defendingPlayerName} for ${damage} damage`
}
/*const attack = function(attackingPlayer, defendingPlayer, baseDamage, variableDamage)
{
let damage = baseDamage + calculateVariableDamage(variableDamage)
defendingPlayer.health -= damage;
console.log(printAttackMessage(attackingPlayer, defendingPlayer, damage))
}*/


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

const player2 = {
name : 'Saruman',
health : 10
}


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


const attack = (attackingPlayer, defendingPlayer, baseDamage, variableDamage) =>
{
let damage = baseDamage + calculateVariableDamage(variableDamage)
defendingPlayer.health -= damage;
console.log(createAttackMessage(attackingPlayer.name, defendingPlayer.name, damage))
}

// DO NOT MODIFY THE CODE BELOW THIS LINE
// Set attacker and defender. Reverse roles each iteration
Expand Down
11 changes: 10 additions & 1 deletion itemizedReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
// with these properties: {descr, price}
// i.e. {descr: 'Coke', price: 1.99}
// function should log each item to the console and log a total price

const logReceipt = function(...args)
{
let total = 0
for (i = 0; i < args.length; i++)
{
total += args[i].price
console.log(`${args[i].descr} - $${args[i].price}`)
}
console.log(`Total - ${total}`)
}


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

const testPhoneNumber = function(phoneNumber)
{
let phoneRegex = /^(\(?(\d{3})\)?)[ -]?(\d{3})[ -]?(\d{4})$/
return phoneRegex.test(phoneNumber)
}


// Explanation of RegExp
Expand All @@ -29,7 +33,18 @@ console.log(testPhoneNumber('(206) 33-4444')); // should return false, missing a
// 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 = function(phoneNumber)
{
let phoneMatches = /\d{3,4}/g
const phoneComponents = []
i = 0
while((phArray = phoneMatches.exec(phoneNumber)) != null )
{
phoneComponents[i++] = phArray[0]
console.log(phoneComponents)
}
return {areaCode: phoneComponents[0], phoneNumber: `${phoneComponents[1]}${phoneComponents[2]}`}
}


// Check parsePhoneNumber
Expand Down
18 changes: 16 additions & 2 deletions soccer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ const getPointsFromResult = function getPointsFromResult(result) {
// 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 totalPoints = 0
for (i = 0; i < results.length; i++)
{
totalPoints += getPointsFromResult(results[i])
}
return totalPoints
}


// Check getTotalPoints
Expand All @@ -29,7 +37,13 @@ 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 = function (...args)
{
for (j = 0; j < args.length; j++)
{
console.log(`${args[j].name}: ${getTotalPoints(args[j].results)}`)
}
}


// Check orderTeams
Expand Down
19 changes: 18 additions & 1 deletion spaceShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,25 @@
// - 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()
{
console.log(`${this.name} moving to ${this.topSpeed}`)
}
}


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

const enterprise = new SpaceShip("Enterprise", "maximum warp")
enterprise.accelerate()

const milleniumFalcon = new SpaceShip("Millenium Falcon", "light speed")
milleniumFalcon.accelerate()