Skip to content
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

adding oop calc to resilient coders branch #6

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
Binary file added .DS_Store
Binary file not shown.
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# 🔢 Week01 Alumni Project: Calculator
# OOP Calculator
A calculator done using object-oriented programming.

### Goal: Build a Simple Calculator using JS OOP best practices
**Link to project:**
https://quirky-aryabhata-167d92.netlify.com/

### What it should look like:
![alt tag](oop_calc.png)

![Calculator](calculator.jpg)
## How It's Made:

### How to submit your code for review:
**Tech used:** little bit of HTML, CSS & a lot of Javascript

- Fork and clone this repo
- Create a new branch called answer
- Checkout answer branch
- Push to your fork
- Issue a pull request
- Your pull request description should contain the following:
- (1 to 5 no 3) I completed the challenge
- (1 to 5 no 3) I feel good about my code
- Anything specific on which you want feedback!
So I took a previous version of a JS calculator and I converted it into an object-oriented program.

Example:
```
I completed the challenge: 5
I feel good about my code: 4
I'm not sure if my constructors are setup cleanly...
```
## Lessons Learned:

This was the first project i did with OOP so this helped me with learning the structure and overall syntax of OOP.

## Examples:
Take a look at these couple examples that I have in my own portfolio:

**Palettable:** https://github.com/alecortega/palettable

**Twitter Battle:** https://github.com/alecortega/twitter-battle

**Patch Panel:** https://github.com/alecortega/patch-panel
Binary file added css/.DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*{
box-sizing: border-box;
}
html,body{
margin: 0;
padding: 0;
}
27 changes: 27 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css">
<title></title>
</head>
<body>
<input id="val1" type="text" name="" value="">
<input id="val2" type="text" name="" value="">
<section id="add">
+
</section>
<section id="sub">
-
</section>
<section id="multi">
*
</section>
<section id="divi">
/
</section>
<h2 id="h2">Hello, World!</h2>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="js/main.js" type="text/javascript"></script>
</body>
</html>
Binary file added js/.DS_Store
Binary file not shown.
70 changes: 70 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// objects come from here
class Calculator {

// Add two values together and returns the sum
add (val1, val2) {
var sum = val1 + val2
return sum
}
// subtract two values together and returns the difference
subtract (val1, val2) {
var diff = val1 - val2
return diff
}
// multiply two values together and returns the product
multiply (val1, val2) {
var prod = val1 * val2
return prod
}
// divides two values together and returns the quotent
divide (val1, val2) {
var quo = val1 / val2
return quo
}
};

//load calc when page loads
const calc = new Calculator();

// event listers for 4 different functions
document.getElementById('add').onclick = addVal
document.getElementById('sub').onclick = subVal
document.getElementById('multi').onclick = divVal
document.getElementById('divi').onclick = multiVal

//
function addVal(){
var val1 = parseInt(document.getElementById('val1').value)
var val2 = parseInt(document.getElementById('val2').value)
var sum = calc.add(val1, val2)

// displays sum to DOM
document.getElementsByTagName('h2')[0].innerHTML = sum
}

function subVal(){
var val1 = parseInt(document.getElementById('val1').value)
var val2 = parseInt(document.getElementById('val2').value)
var diff = calc.subtract(val1, val2)

// displays difference to DOM
document.getElementsByTagName('h2')[0].innerHTML = diff
}

function divVal(){
var val1 = parseInt(document.getElementById('val1').value)
var val2 = parseInt(document.getElementById('val2').value)
var quotent = calc.multiply(val1, val2)

// displays quotent to DOM
document.getElementsByTagName('h2')[0].innerHTML = quotent
}

function multiVal(){
var val1 = parseInt(document.getElementById('val1').value)
var val2 = parseInt(document.getElementById('val2').value)
var product = calc.divide(val1, val2)

// displays product to DOM
document.getElementsByTagName('h2')[0].innerHTML = product
}
Binary file added oop_calc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.