Skip to content

Week-3 Assignment Joann Cahill #3

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

Closed
wants to merge 5 commits into from
Closed
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,14 +1,43 @@
// 1. Create attack function below. This will take the following parameters:
// attackingPlayer, defendingPlayer, baseDamage, variableDamage

// const attack2 = function (attackingPlayer, defendingPlayer, baseDamage, variableDamage) {

// const randomValue = Math.floor(Math.random() * variableDamage);
// const totalDamage = baseDamage + randomValue;
// defendingPlayer.health = defendingPlayer.health - totalDamage;

// console.log( `${attackingPlayer.name}' hits '${defendingPlayer.name} 'for '${totalDamage}' damage'`)
// }


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

let player2 = {
name: 'Jane',
health: 10,
};

// console.log(player1)
// console.log(player2)

// 3. Refactor attack function to an arrow function. Comment out function above.
const attack = (attackingPlayer, defendingPlayer, baseDamage, variableDamage) => {
const randomValue = Math.floor(Math.random() * variableDamage);
const totalDamage = baseDamage + randomValue;
defendingPlayer.health = defendingPlayer.health - totalDamage;

// console.log( randomValue );
// console.log( totalDamage );
// console.log( defendingPlayer.health );
console.log(`${attackingPlayer.name}' hits '${defendingPlayer.name} 'for '${totalDamage}' damage'`)
}



Expand All @@ -23,7 +52,6 @@ while (player1.health >= 1 && player2.health >= 1 && preventInfiniteLoop > 0) {
const [attackingPlayer, defendingPlayer] = attackOrder;
console.log(attack(attackingPlayer, defendingPlayer, 1, 2));
attackOrder = attackOrder.reverse();

preventInfiniteLoop--;
}
const eliminatedPlayer = player1.health <= 0 ? player1 : player2;
Expand Down
47 changes: 40 additions & 7 deletions itemizedReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,47 @@



// Check
logReceipt(
{ descr: 'Burrito', price: 5.99 },
{ descr: 'Chips & Salsa', price: 2.99 },
{ descr: 'Sprite', price: 1.99 }
);
// Create an array of menu item objects
const items = [{
descr: 'Burrito',
price: 5.99
},
{
descr: 'Chips & Salsa',
price: 2.99
},

{
descr: 'Mexi Fries',
price: 1.99
},

{
descr: 'Sprite',
price: 1.99
}

]

function logReceipt() {
//set total cost to 0
let totalCost = 0;

//create list of items purchased and their price
items.forEach(item => console.log(`${item.descr} - $${item.price}`))

//create total line
items.forEach(item => totalCost += item.price);
console.log(`Total Cost - $${totalCost}`)

};

logReceipt();



// should log something like:
// Burrito - $5.99
// Chips & Salsa - $2.99
// Sprite - $1.99
// Total - $10.97
// Total - $10.97
40 changes: 38 additions & 2 deletions phoneNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,52 @@
// $ end of line

// check testPhoneNumber

console.log('Result for validation of phone number')

function testPhoneNumber(phone) {
const regex = /^\(\d{3}\)[-\s]\d{3}[-\s]\d{4}$/;

if (phone.match(regex)) {
console.log('true');
} else {
console.log('false');

}

};

console.log('Phone number is valid')
console.log(testPhoneNumber('(206) 333-4444')); // should return true
console.log('Phone number is not valid')
console.log(testPhoneNumber('(206) 33-4444')); // should return false, missing a digit


// 1. Create a function parsePhoneNumber that takes in a phoneNumber string
// in one of the above formats. For this, you can *assume the phone number
// in one of the above fparsePhoneNumberormats. For this, you can *assume the phone number
// passed in is correct*. This should use a regular expression
// 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}
console.log('Result for parsing of phone number');

function parsePhoneNumber(phone) {

//parse area code
const regexAreaCode = /\d{3}/g;
let parsedAreaCode = regexAreaCode.exec(phone);
// console.log(parsedAreaCode);


//find phone nubers
const regexPhone = /\d{3}[-\s]\d{4}/;
let parsedPhone = regexPhone.exec(phone);
let parsedPhone2 = parsedPhone[0].replace('-', '');
// console.log(parsedPhone);
// console.log(parsedPhone2);

let phoneNumbers = (`areaCode: ${parsedAreaCode}, phoneNumber: ${parsedPhone2} `)
console.log(phoneNumbers);
};



Expand Down
117 changes: 117 additions & 0 deletions phoneNumberBU.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Create a function testPhoneNumber
// takes in a phoneNumber string in one of these formats:
// '(206) 333-4444'
// '206-333-4444'
// '206 333 4444'
// Returns true if valid, false if not valid



// Explanation of RegExp
// ^ start of line
// \( optional start parenthesis
// \d{3} exactly 3 digit characters
// \) optional end parenthesis
// [-\s] one of a space or a dash
// \d{3} exactly 3 digit characters
// [-\s] one of a space or a dash
// \d{4} exactly 4 digit characters
// $ end of line

// check testPhoneNumber

console.log('Result for validation of phone number')

function testPhoneNumber(phone) {
const regex = /^\(\d{3}\)[-\s]\d{3}[-\s]\d{4}$/;

if (phone.match(regex)) {
console.log('true');
} else {
console.log('false');

}

};

console.log('Phone number is valid')
console.log(testPhoneNumber('(206) 333-4444')); // should return true
console.log('Phone number is not valid')
console.log(testPhoneNumber('(206) 33-4444')); // should return false, missing a digit

// 1. Create a function parsePhoneNumber that takes in a phoneNumber string
// in one of the above fparsePhoneNumberormats. For this, you can *assume the phone number
// passed in is correct*. This should use a regular expression
// 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}\\
console.log('Result for parsing of phone number');

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
}

console.log('Result for parsing of phone number');

/\(?\d{3}\)?/
function parsePhoneNumber(phone) {
//parse area code
const regexAreaCode = (/\d{3}/g);
let parsedAreaCode = regexAreaCode.exec(phone);
console.log(parsedAreaCode);



//parse phone wo area code
const regexPhonePrefix = (/\d{3}/g);
const regexPhoneExt = (/\d{4}/);
const regexDigit = (/[0-9]+/);


let parsedAreaCode = regexAreaCode.exec(phone);
let parsedPhonePrefix = regexPhonePrefix.exec(phone);
let parsedPhoneExt = regexPhoneExt.exec(phone);
let parsedPhone = regexPhone.exec(phone);
let parsedDigit = regexDigit.exec(phone);
let AreaCode = `${parsedAreaCode}`;
// let parsedPhoneNumber = `${parsedPhonePrefix} ${parsedPhoneExt}`;

let phoneNumber = parsedPhone.replace("-", "");
//var strNewWebsiteName = strWebsiteName.replace("\n", "");

console.log(parsedPhonePrefix);
console.log(parsedPhoneExt);
console.log(parsedPhone);
console.log(parsedDigit);
console.log(AreaCode);
console.log(phoneNumber);
// console.log(parsedPhoneNumber);


console.log(`areaCode: (${AreaCode}), phoneNumber: ${phoneNumber} `);


};




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

console.log(parsePhoneNumber('(222) 422-5353'));
// returns {areaCode: '222', phoneNumber: '4225353'}
44 changes: 39 additions & 5 deletions soccer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const RESULT_VALUES = {
w: 3,
d: 1,
Expand All @@ -16,27 +15,62 @@ 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

function getTotalPoints(gameResults) {
let gameResultsSplit = gameResults.split('');
let total = 0;
gameResultsSplit.forEach((gameResult) => {
if (RESULT_VALUES[gameResult]) {
total += RESULT_VALUES[gameResult];
}

})
//console.log(total);
return total

}


// Check getTotalPoints
console.log(getTotalPoints('wwdl')); // should equal 7
console.log(getTotalPoints('wwldlwd')); // should equal 11
console.log(getTotalPoints('wzldlwd')); // should equal 8

// 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"


function orderTeams(...args) {
args.forEach((arg) => {
let gameResultsSplit = arg.results.split('');
let total = 0;
gameResultsSplit.forEach((gameResult) => {
if (RESULT_VALUES[gameResult]) {
total += RESULT_VALUES[gameResult];
}
})
console.log(`${arg.name}: ${total}`);
})
}


// Check orderTeams
orderTeams(
{ name: 'Sounders', results: 'wwdl' },
{ name: 'Galaxy', results: 'wlld' }
);
orderTeams({
name: 'Sounders',
results: 'wwdl'
}, {
name: 'Galaxy',
results: 'wlld'
}, {
name: 'Minnesota United',
results: 'dldw'
});
// should log the following to the console:
// Sounders: 7
// Galaxy: 4
18 changes: 18 additions & 0 deletions spaceShip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@
// - 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;

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

}


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

const spaceX = new SpaceShip('Dragon', '5,770 mph');
spaceX.usetopSpeed();

const blueOrigin = new SpaceShip('New Shepard', '2,217 mph');
blueOrigin.usetopSpeed();