Skip to content

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

Open
wants to merge 3 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
52 changes: 49 additions & 3 deletions battleGame.js
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(`
Copy link
Contributor

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 logs undefined 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.

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
Expand All @@ -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)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logs undefined instead of a string that is returned by the function.

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!`);
4 changes: 2 additions & 2 deletions itemizedReceipt.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<body>
<h1>Open console to view output</h1>

<script src="itemizedReceipt.js"></script>
<script src="v2itemizedReciept.js"></script>
</body>

</html>
</html>
64 changes: 59 additions & 5 deletions itemizedReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Copy link
Contributor

Choose a reason for hiding this comment

The 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 R you might create the impression that is is a Class, not a function.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 logReceipt() function. Keep in mind with these starter files that you don't want to change the part that is already there . . . The setup wants to run a function named logReceipt() that you are tasked with writing, and then give it those provided values.

So a couple lessons here . . . logReceipt() is meant to be a reusable piece of code that can take any number of given parameters. This is the core of writing functions, being able to use that bit of code over and over again with variable inputs. One key tool that needs to be used is a spread operator, so that it will work if given 1,2,3, or 1000 menu items.

What's happened here instead is making logReceipt() just a static array, and not a function at all, then getting the desired output with a static, single-purpose function Receipt() that can only ever do this one job.

Try again, simplify things, and let the boilerplate run logReceipt() as a function that takes the three objects.

66 changes: 57 additions & 9 deletions phoneNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@
// Returns true if valid, false if not valid


const basicPhoneRegex =/^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$/g;
// This regex allows for a max of 3 OPTIONAL international code, a Strict Regular 10 digits, and OPTIONAL ext number between 1 and 3 numbers but to validate the extentision number it requeires to be set in a specific manner
// ext. ||ext: || Ext. || Ext: proceeded by the number
const advancePhoneRegex = /(\+\d{1,3}\s?)?((\(\d{3}\)\s?)|(\d{3})(\s|-?))(\d{3}(\s|-?))(\d{4})(\s?(([E|e]xt[:|.|]?)|x|X)(\s?\d+))?/g

//im using e as a parameter because when inspect pages and read their JS they use single letter parameters for inputs
function testPhoneNumber(e){
if(e.match(advancePhoneRegex)){
console.log(`
${e} \u2713 Valid Phone Number`)
}else{
console.log(`
${e} \u2717 Invalid Phone Number`)
}
}


// Explanation of RegExp
// ^ start of line
Expand All @@ -19,8 +35,29 @@
// $ 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
testPhoneNumber('(206) 342-2321 Ext. 1')
testPhoneNumber('(206) 342-2321 Ext: 1')
testPhoneNumber('(206) 342-2321 Ext: 12')
testPhoneNumber('(206) 342-2321 Ext. 12')
testPhoneNumber('(206) 342-2321 Ext: 123')
testPhoneNumber('(206) 342-2321 ext. 1')
testPhoneNumber('(206) 342-2321 ext: 1')
testPhoneNumber('(206) 342-2321 ext: 12')
testPhoneNumber('(206) 342-2321 ext. 12')
testPhoneNumber('(206) 342-2321 ext: 123')
testPhoneNumber('011 (206) 342-2321 Ext. 1')
testPhoneNumber('1 (206) 342-2321 Ext: 1')
testPhoneNumber('12 (206) 342-2321 Ext: 12')
testPhoneNumber('(206) 342-2321 Ext. 12')
testPhoneNumber('(206) 342-2321 Ext: 123')
testPhoneNumber('(206) 342-2321 ext. 1')
testPhoneNumber('(206) 342-2321 ext: 1')
testPhoneNumber('(206) 342-2321 ext: 12')
testPhoneNumber('(206) 342-2321 ext. 12')
testPhoneNumber('(206) 342-2321 ext: 123')
testPhoneNumber('(206) 333-4444'); // should return true
testPhoneNumber('(206) 33-4444'); // should return false, missing a digit
testPhoneNumber('206 333-4444') //Edgecase if no paranthases are given by the user


// 1. Create a function parsePhoneNumber that takes in a phoneNumber string
Expand All @@ -29,12 +66,23 @@ 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}
function parsePhoneNumber(pNumber){
const regexArea =/\d{3,4}/;
const regexPhone =/\d{3}-\d{4}/;
let testArea = regexArea.exec(pNumber)
let testPhone = regexPhone.exec(pNumber)
let Object ={
area: testArea[0],
Phone: testPhone[0]
}
console.log(Object)

}
parsePhoneNumber('(206) 333-4444')

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


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

console.log(parsePhoneNumber('(222) 422-5353'));
// returns {areaCode: '222', phoneNumber: '4225353'}
parsePhoneNumber('(222) 422-5353');
// // returns {areaCode: '222', phoneNumber: '4225353'}
25 changes: 24 additions & 1 deletion soccer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't overlook the helpers provided. See how the object RESULT_VALUES and function getPointsFromResult are here to do some of this work for you?

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
Expand All @@ -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' },
Expand Down
29 changes: 29 additions & 0 deletions spaceShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,36 @@
// - 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(){
let idle =0;
while(idle<this.topSpeed){
idle+=1;
console.log(idle)
}
console.log('top speed reached', this.topSpeed)
}
}


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

const ship1 = new SpaceShip("ship1", '15')
const ship2 = new SpaceShip("ship1", '155')
const ship3 = new SpaceShip("ship1", '1555')

ship1.accelerate()
ship2.accelerate()
// uncomment and run them with node
// console.log(ship1)
// setTimeout(() => {console.log("\n LAUNCHING IN")}, 1000);
// setTimeout(() => {console.log("\n 3")}, 2000);
// setTimeout(() => {console.log("\n 2")}, 3000);
// setTimeout(() => {console.log("\n 1")}, 4000);
// setTimeout(() => {console.log("\n ")}, 5000);
// setTimeout(() => {ship1.accelerate()}, 6000);
42 changes: 42 additions & 0 deletions v2itemizedReciept.js
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