Skip to content

Commit

Permalink
Merge pull request #54 from MrDrops/main
Browse files Browse the repository at this point in the history
Geometry, cash register, bank
  • Loading branch information
carterdeacon authored Mar 11, 2022
2 parents b6089e6 + 47cdd48 commit efd2c5c
Show file tree
Hide file tree
Showing 3 changed files with 383 additions and 0 deletions.
129 changes: 129 additions & 0 deletions Pedro Vivas/wk01 - starts 7th Mar/4-thu/exercises.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
//SOURCE
https://gist.github.com/wofockham/dacf2da17c743afb2b17

# Geometry Function Lab

### Part 1, Rectangle

Given the following a `rectangle` object like the one below, write the following functions which accept a `rectangle` object as an argument:

* isSquare - Returns whether the rectangle is a square or not
* area - Returns the area of the rectangle
* perimeter - Returns the perimeter of the rectangle

```javascript
const rectangleA = {
length: 4,
width: 4
};
```

### Part 2, Triangle

Given the following a `triangle` object like the one below, write the following functions which accept a `triangle` object as an argument:

* isEquilateral - Returns whether the triangle is equilateral or not
* isIsosceles - Returns whether the triangle is isosceles or not
* area - Returns the area of the Triangle
* isObtuse - Returns whether the triangle is obtuse or not

```javascript
const triangleA = {
sideA: 3,
sideB: 4,
sideC: 4
};
```


# The Cash Register

Write a function called cashRegister that takes a shopping cart object. The object contains item names and prices (itemName: itemPrice). The function should return the total price of the shopping cart.
Example

```
// Input
const cartForParty = {
banana: "1.25",
handkerchief: ".99",
Tshirt: "25.01",
apple: "0.60",
nalgene: "10.34",
proteinShake: "22.36"
};

// Output
cashRegister(cartForParty)); // 60.55
```


# Credit Card Validation

You're starting your own credit card business. You've come up with a new way to validate credit cards with a simple function called validateCreditCard that returns true or false.

Here are the rules for a valid number:

- Number must be 16 digits, all of them must be numbers
- You must have at least two different digits represented (all of the digits cannot be the same)
- The final digit must be even
- The sum of all the digits must be greater than 16

The following credit card numbers are valid:

- `9999-9999-8888-0000`
- `6666-6666-6666-1666`

The following credit card numbers are invalid:

- `a923-3211-9c01-1112` invalid characters
- `4444-4444-4444-4444` only one type of number
- `1111-1111-1111-1110` sum less than 16
- `6666-6666-6666-6661` odd final number

## Example
```
validateCreditCard('9999-9999-8888-0000'); // Returns: true
```

*Hint*: Remove the dashed from the input string before checking if the input credit card number is valid.

*Bonus*: Return an object indicating whether the credit card is valid, and if not, what the error is

```
{ valid: true, number: 'a923-3211-9c01-1112' }
{ valid: false, number: 'a923-3211-9c01-1112', error: ‘wrong_length’ }
```

*Double Bonus*: Make your credit card scheme even more advanced! What are the rules, and what are some numbers that pass or fail? Ideas: check expiration date! Check out the Luhn Algorithm for inspiration.


# JavaScript Bank

In this homework, you'll create a basic `bank` in Javascript. The bank has many `accounts` and the following capabilities that you need to write.

#### Bank

There is only one bank. This bank has an array of accounts. The bank needs a method that will return the total sum of money in the accounts. It also needs an `addAccount` method that will enroll a new account at the bank and add it to the array of accounts. There is no need to create additional functions of the bank to delete accounts, etc.

The bank has many accounts. Accounts should be objects that all share a set of common functionality.

#### Accounts

Accounts have a current balance and owner's name. You should be able to deposit or withdraw from an account to change the balance.

There is no need to write a user interface. Make sure functions return values -- you may also have your functions `console.log()` values to help you see your code working.

You should write a basic story through a series of JavaScript commands that shows that the methods do indeed work as expected: add some accounts, show the total balance, make some deposits and withdrawals, show the new total balance.

### Tips

Don't overthink this. Shorter code is probably the answer.

## Bonus

- Ensure that the accounts cannot have negative values.
- Write a 'transfer' on the bank that allows you to transfer amounts between two accounts.

## Additional

Begin exploring the [JavaScript Koans](https://github.com/liammclennan/JavaScript-Koans). Fork, clone and start trying them.
13 changes: 13 additions & 0 deletions Pedro Vivas/wk01 - starts 7th Mar/4-thu/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="script.js" async></script>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Homework: Objects</h1>
</body>
</html>
241 changes: 241 additions & 0 deletions Pedro Vivas/wk01 - starts 7th Mar/4-thu/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
//Geometry function lab
console.log("Part 1: Rectangle");
console.log("_________________");
const rectangleA = {
length: 4,
width: 4
};

const rectangleB = {
length: 4,
width: 8
};

const isSquare = function(rect) {
return rect.length === rect.width;
}

const area = function(rect) {
return rect.length * rect.width;
}

const perimeter = function(rect) {
return (rect.length * 2) + (rect.width * 2);
}

//TEST CASES
console.log(isSquare(rectangleA)); //square
console.log(isSquare(rectangleB)); //not square
console.log(area(rectangleA)); // 4*4
console.log(area(rectangleB)); // 4*8
console.log(perimeter(rectangleA)); // (4*2) + (4*2)
console.log(perimeter(rectangleB)); // (4*2) + (8*2)


//Triangle
console.log("PART 2: Triangles");
console.log("_________________");
const triangleA = {
sideA: 3,
sideB: 4,
sideC: 4
};

const triangleB = {
sideA: 4,
sideB: 4,
sideC: 4
};

//obtuse triangle
const triangleC = {
sideA: 3,
sideB: 4,
sideC: 6
};

//Equilateral triangle = all sides same length
const isEquilateral = function(triObj) {
return triObj.sideA === triObj.sideB && triObj.sideB === triObj.sideC;
};

//Isosceles triangle = 2 sides equal length
const isIsosceles = function(triObj) {
return triObj.sideA === triObj.sideB || triObj.sideA === triObj.sideC || triObj.sideB === triObj.sideC;
};
//Heron's area of triangle:
//area of triangle = Sqr of s(s-a)(s-b)(s-c), where s = (a+b+c)/2; a,b,c = side length;
const triArea = function(triObj) {
let s = (triObj.sideA + triObj.sideB + triObj.sideC) / 2;
let toSq = s * (s - triObj.sideA) * (s - triObj.sideB) * (s - triObj.sideB);
let ans = Math.sqrt(toSq);
return ans;
};

//Obtuse triangle = The sides of an obtuse triangle should satisfy the condition that the sum of the squares of any two sides is lesser than the square of the third side.
const isObtuse = function(triObj) {
let squareA = Math.pow(triObj.sideA, 2);
let squareB = Math.pow(triObj.sideB, 2);
let squareC = Math.pow(triObj.sideC, 2);
let arr = [squareA, squareB, squareC].sort((a,b)=> a-b); // = function(a,b) {return a - b};
return arr[0] + arr[1] < arr[2];
};
//TEST CASES
console.log("isEquilateral");
console.log(isEquilateral(triangleA)); // not equilateral
console.log(isEquilateral(triangleB)); // equilateral
console.log(isEquilateral(triangleC)); // not equilateral
console.log("isIsosceles");
console.log(isIsosceles(triangleA)); // isosceles
console.log(isIsosceles(triangleB)); // isosceles
console.log(isIsosceles(triangleC)); // not isosceles
console.log("find Area");
console.log(triArea(triangleA));
console.log(triArea(triangleB));
console.log(triArea(triangleC));
console.log("isObtuse");
console.log(isObtuse(triangleA)); //not obtuse
console.log(isObtuse(triangleB)); //not obtuse
console.log(isObtuse(triangleC)); //obtuse


//Cash register
console.log("Cash Register");
console.log("_________________");
const cart1 = {
banana: "1.25",
handkerchief: ".99",
Tshirt: "25.01",
apple: "0.60",
nalgene: "10.34",
proteinShake: "22.36"
}

const cart2 = {
soda: "2.20",
chips: "3.40",
brownies: "10.50",
cake: "25",
iceCream: "12.90"
}

const cashRegister = function(shopCart) {
let sum = 0;
for(let item in shopCart) {
let price = shopCart[item];
sum += parseFloat(price);
}
return sum;
}

//TEST CASES
console.log(cashRegister(cart1)); // total = 60.55
console.log(cashRegister(cart2)); // total = 54.00

//Credit Card Validation
const validateCreditCard = function(cc) {
//remove -
//must be 16 chars, all numbers
//final digit must be even
//sum of all digits must be greater than 16
//return { valid: false, number: 'a923-3211-9c01-1112', error: ‘wrong_length’ }
const errorType = [
"Must be 16 digits, all numbers",
"Final digit not even",
"Sum of all digits cannot not be < 16"
]
let validation = {
valid: true,
number: cc,
error: 'typeOf'
};
//remove "-"
const stripCc = cc.split("-").join("");
// check length
if(stripCc.length < 16) {
validation.error = errorType[0];
validation.valid = false;
return validation;
}
// last number is even
if( parseInt(stripCc[15]) % 2 != 0) {
validation.error = errorType[1];
validation.valid = false;
return validation;
}
// numbers only
let sum = 0;
for(let j = 0; j < stripCc.length; j++) {
if((/[a-zA-Z]/).test(stripCc[j])) {
validation.error = errorType[0];
validation.valid = false;
return validation;
}
//add all numbers
sum += parseInt(stripCc[j]);
}
//if sum of all numbers < 16
if (sum < 16) {
validation.error = errorType[2];
validation.valid = false;
}

return validation;
}

console.log(validateCreditCard('a923-3211-9c01-1112')); // characters not all numbers
console.log(validateCreditCard('9923-3211-9101-1111')); // last digit not even
console.log(validateCreditCard('7923-3211-9501-111')); // less than 16 numbers
console.log(validateCreditCard('1111-1111-1101-1110')); // sum of numbers < 16


//Javascript Bank
console.log("Javascript Bank");
console.log("_________________");

let accounts = [{owner: "John Doe", balance: 2500}, {owner: "Maddison Li", balance: 4500}];

const addAccount = function(name, startBalance) {
//params: str, number
accounts.push({owner: name, balance: startBalance});
return `new account added: owner: ${name}, starting balance: ${startBalance}`;
};

const deposit = function(accOwner, cashToAdd) {
let receipt = [];
for (let i = 0; i < accounts.length; i++) {
let name = accounts[i].owner;
let currBalance = accounts[i].balance;
if (accOwner === name) {
accounts[i].balance = currBalance + cashToAdd;
receipt = [cashToAdd, name, accounts[i].balance];
break;
}
}
return `${receipt[0]} added to ${receipt[1]}'s account. new Balance: ${receipt[2]}`;
};

const withdraw = function(accOwner, cashWithdraw) {
let receipt = [];
for (let i = 0; i < accounts.length; i++) {
let name = accounts[i].owner;
let currBalance = accounts[i].balance;
if (accOwner === name) {
accounts[i].balance = currBalance - cashWithdraw;
receipt = [cashWithdraw, name, accounts[i].balance];
break;
}
}
return `${receipt[0]} withdrawn from ${receipt[1]}'s account. new Balance: ${receipt[2]}`;
};

//TEST CASES
console.log(accounts.length); // check number of accounts;
console.log(addAccount("Jamie Lanister", 12000)); //Add new account
console.log(accounts.length); // check new account added;
console.log(deposit("John Doe", 500)); // deposit money to an account
console.log(withdraw("Maddison Li", 1300)); // withdraw money from an account
console.log(accounts); //check deposit and withdraw from accounts


//?? withdraw and deposit not working yet!!!

0 comments on commit efd2c5c

Please sign in to comment.