-
Notifications
You must be signed in to change notification settings - Fork 21
HW complete #18
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
base: master
Are you sure you want to change the base?
HW complete #18
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,59 @@ | ||
|
||
|
||
// 1. Create attack function below. This will take the following parameters: | ||
// attackingPlayer, defendingPlayer, baseDamage, variableDamage | ||
|
||
// let variableDamage = Math.random()*10 | ||
// const baseDamage = 1; | ||
// const attack = function(attackingPlayer, defendingPlayer, baseDamage, variableDamage){ | ||
|
||
// // this will allow playability vs one player dominating | ||
// if(variableDamage>5){ | ||
// variableDamage=5; | ||
// } | ||
// defendingPlayer - (baseDamage + variableDamage) | ||
// let totalDamage = baseDamage + variableDamage | ||
|
||
// console.log(` | ||
// Attacking Player: ${attackingPlayer.name} | ||
// Defending Player: ${defendingPlayer.name} | ||
|
||
// Defending Player recived ${totalDamage} in damage | ||
// Defending Player Health: ${defendingPlayer.health} | ||
|
||
// Attacking Player Health: ${attackingPlayer.health}`) | ||
// } | ||
|
||
// 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: 'Player 1', | ||
health: 10, | ||
} | ||
let player2 = { | ||
name: 'Player 2', | ||
health: 10, | ||
} | ||
|
||
// 3. Refactor attack function to an arrow function. Comment out function above. | ||
const attack = (attackingPlayer, defendingPlayer, baseDamage, variableDamage) =>{ | ||
// this line avoids single move wins | ||
if(variableDamage > 9){ | ||
variableDamage = Math.floor(Math.random() *10) | ||
} | ||
defendingPlayer.health -= (baseDamage + variableDamage) | ||
let totalDamage = baseDamage + variableDamage | ||
|
||
console.log(` | ||
Attacking Player: ${attackingPlayer.name} | ||
Defending Player: ${defendingPlayer.name} | ||
|
||
Defending Player recived ${totalDamage} in damage | ||
Defending Player Health: ${defendingPlayer.health} | ||
|
||
Attacking Player Health: ${attackingPlayer.health}`) | ||
} | ||
|
||
|
||
// DO NOT MODIFY THE CODE BELOW THIS LINE | ||
|
@@ -21,10 +65,12 @@ let attackOrder = [player1, player2]; | |
let preventInfiniteLoop = 100; | ||
while (player1.health >= 1 && player2.health >= 1 && preventInfiniteLoop > 0) { | ||
const [attackingPlayer, defendingPlayer] = attackOrder; | ||
console.log(attack(attackingPlayer, defendingPlayer, 1, 2)); | ||
// had to update the parameter for variable damage so it could be truely random | ||
console.log(attack(attackingPlayer, defendingPlayer, 1, Math.floor(Math.random() *10))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this logs |
||
attackOrder = attackOrder.reverse(); | ||
|
||
preventInfiniteLoop--; | ||
} | ||
const eliminatedPlayer = player1.health <= 0 ? player1 : player2; | ||
console.log(`${eliminatedPlayer.name} has been eliminated!`); | ||
console.log(` | ||
${eliminatedPlayer.name} has been eliminated!`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,67 @@ | |
|
||
|
||
// Check | ||
logReceipt( | ||
{ descr: 'Burrito', price: 5.99 }, | ||
{ descr: 'Chips & Salsa', price: 2.99 }, | ||
{ descr: 'Sprite', price: 1.99 } | ||
); | ||
let logReceipt =[ | ||
{ descr: 'Burrito', | ||
price: 5.99, | ||
type: 'Food' | ||
}, | ||
{ descr: 'Chips & Salsa', | ||
price: 2.99, | ||
type: 'Food' | ||
}, | ||
{ descr: 'Sprite', | ||
price: 1.99, | ||
type:'Drink', | ||
oz: 12, | ||
sugar: true | ||
} | ||
] | ||
; | ||
// should log something like: | ||
// Burrito - $5.99 | ||
// Chips & Salsa - $2.99 | ||
// Sprite - $1.99 | ||
// Total - $10.97 | ||
|
||
// source: https://dor.wa.gov/get-form-or-publication/publications-subject/tax-topics/seattle-s-sweetened-beverage-tax | ||
const seattleSugarTaxRate = .0175 | ||
|
||
//source: http://www.salestaxstates.com/sales-tax-calculator-washington-seattle-98101 | ||
const washingtonSalesTax = .065 | ||
const seattleSaleTax = .0375 | ||
const wholeSalesTax = washingtonSalesTax + seattleSaleTax | ||
|
||
function Receipt(){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember the good naming conventions, variables and functions ought to always begin with a lowercase letter and use camelCase. By naming this function with a capital |
||
let sugarTaxTotal = 0; | ||
let totalBeforeTax = 0; | ||
let totalSalesTax = 0; | ||
totalWithTax = 0; | ||
console.log(` | ||
THANK YOU FOR SHOPPING AT CARLITO's | ||
----------------------------------------------------------------- | ||
ITEM - PRICE`) | ||
for(let i =0; i<logReceipt.length; i++){ | ||
if(logReceipt[i].type == 'Drink'){ | ||
if(logReceipt[i].sugar == true){ | ||
sugarTaxTotal += logReceipt[i].oz * seattleSugarTaxRate | ||
} | ||
} | ||
totalBeforeTax +=logReceipt[i].price | ||
totalSalesTax +=logReceipt[i].price*wholeSalesTax | ||
console.log(` | ||
${logReceipt[i].descr} - $${logReceipt[i].price}`) | ||
} | ||
totalWithTax += totalBeforeTax + totalSalesTax + sugarTaxTotal | ||
console.log(` | ||
----------------------------------------------------------------- | ||
Amount: $${totalBeforeTax} | ||
Sales Tax: $${totalSalesTax.toFixed(2)} | ||
Seattle's Sugar Tax: $${sugarTaxTotal.toFixed(2)} | ||
----------------------------------------------------------------- | ||
Total: $${totalWithTax.toFixed(2)} | ||
----------------------------------------------------------------- | ||
THANK YOU! | ||
`) | ||
} | ||
Receipt(logReceipt) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The formatting is really nice, but unfortunately this implementation negates the idea of having a So a couple lessons here . . . What's happened here instead is making Try again, simplify things, and let the boilerplate run |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,21 @@ const getPointsFromResult = function getPointsFromResult(result) { | |
// including wins, draws, and losses i.e. 'wwdlw' | ||
// Returns total number of points won | ||
|
||
|
||
function getTotalPoints(string){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't overlook the helpers provided. See how the object |
||
let points = 0; | ||
for(let i = 0; i<string.length;i++){ | ||
if(string[i]=='w'){ | ||
points+=3 | ||
} | ||
if(string[i]=='d'){ | ||
points+=1 | ||
} | ||
if(string[i]=='l'){ // I know this line isn't really neaded just wanted to be more explicit in the code | ||
points+=0 | ||
} | ||
} | ||
return points | ||
} | ||
|
||
// Check getTotalPoints | ||
console.log(getTotalPoints('wwdl')); // should equal 7 | ||
|
@@ -32,6 +46,15 @@ console.log(getTotalPoints('wwdl')); // should equal 7 | |
|
||
|
||
|
||
// This is equal to pythons | ||
// def fName(arg1, *argv): | ||
const orderTeams= (...object) =>{ | ||
object.forEach(object => { | ||
console.log(`${object.name} - ${getTotalPoints(object.results)}`) | ||
}); | ||
} | ||
|
||
|
||
// Check orderTeams | ||
orderTeams( | ||
{ name: 'Sounders', results: 'wwdl' }, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// create function logReceipt that accepts menu items (1 or many) as objects | ||
// 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=(...object)=>{ | ||
let amount =0; | ||
let title =0; | ||
object.forEach(object=>{ | ||
if(title<1){ | ||
console.log(` | ||
Thank you For shopping`) | ||
} | ||
console.log( | ||
` | ||
${object.descr} - ${object.price}` | ||
) | ||
amount+=object.price | ||
title+=1 | ||
}) | ||
let tax =(amount*.1); | ||
let total = (tax + amount) | ||
console.log(` | ||
Amount: ${amount} | ||
Tax: ${tax.toFixed(2)} | ||
Total: ${total.toFixed(2)}` | ||
|
||
) | ||
} | ||
|
||
|
||
// Check | ||
logReceipt( | ||
{ descr: 'Burrito', price: 5.99 }, | ||
{ descr: 'Chips & Salsa', price: 2.99 }, | ||
{ descr: 'Sprite', price: 1.99 } | ||
); | ||
// should log something like: | ||
// Burrito - $5.99 | ||
// Chips & Salsa - $2.99 | ||
// Sprite - $1.99 | ||
// Total - $10.97 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a key idea I wanna point out . . . you want to return a string, not
console.log()
. .. because that's what line 69 is doing! But, as you can see when you run the program, line 69 logsundefined
because this attack() function does not return any value.Keep in mind that console.log() is a tool for developers to see their code while they write. But it's far more important to actually be returning the values our functions calculate.