Some changes on exercise 8. by Stef-Lev · Pull Request #9 · SocialHackersClass10/JavaScript1 · GitHub
Skip to content
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
9 changes: 3 additions & 6 deletions Week1/MAKEME.md
75 changes: 75 additions & 0 deletions Week1/homework/codeAlong/calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<!doctype html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">

<title>Stefanos' Javascript Calculator</title>
<script src="https://kit.fontawesome.com/b039c65688.js" crossorigin="anonymous"></script>
</head>

<body>
<div class="container text-center background">
<div class="row justify-content-center">
<div class="screen m-3 rounded">
<div data-previous-operand class="previous-operand"></div>
<div data-current-operand class="current-operand"></div>
</div>
</div>

<div class="row justify-content-center">
<button type="button" class="btn btn-dark btn-lg m-1 p-auto clear" data-all-clear>AC</button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-delete><i
class="fas fa-backspace"></i></button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-operation>÷</button>
</div>
<div class="row justify-content-center">
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-number>7</button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-number>8</button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-number>9</button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-operation>×</button>
</div>
<div class="row justify-content-center">
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-number>4</button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-number>5</button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-number>6</button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-operation>-</button>
</div>
<div class="row justify-content-center">
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-number>1</button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-number>2</button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-number>3</button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-operation>+</button>
</div>
<div class="row justify-content-center">
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-number>0</button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-number>00</button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto" data-number>.</button>
<button type="button" class="btn btn-dark btn-lg m-1 p-auto equals" data-equals><i
class="fas fa-equals"></i></button>
</div>


</div>

<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"
integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6"
crossorigin="anonymous"></script>
<script src="main.js"></script>
</body>

</html>
122 changes: 122 additions & 0 deletions Week1/homework/codeAlong/calculator/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
class Calculator {
constructor(previousOperandTextElement, currentOperandTextElement) {
this.previousOperandTextElement = previousOperandTextElement
this.currentOperandTextElement = currentOperandTextElement
this.clear()
}

clear() {
this.currentOperand = ""
this.previousOperand = ""
this.operation = undefined
}

delete() {
this.currentOperand = this.currentOperand.toString().slice(0, -1)

}

appendNumber(number) {
if (number === "." && this.currentOperand.includes(".")) return
this.currentOperand = this.currentOperand.toString() + number.toString()
}

chooseOperation(operation) {
if (this.currentOperand === "") return
if (this.previousOperand !== "") {
this.compute()
}
this.operation = operation
this.previousOperand = this.currentOperand
this.currentOperand = ""
}

compute() {
let computation
const prev = parseFloat(this.previousOperand)
const curr = parseFloat(this.currentOperand)
if (isNaN(prev) || isNaN(curr)) return
switch (this.operation) {
case "+":
computation = prev + curr
break;
case "-":
computation = prev - curr
break;
case "×":
computation = prev * curr
break;
case "÷":
computation = prev / curr
break;
default:
return
}
this.currentOperand = computation
this.operation = undefined
this.previousOperand = ""

}

getDisplayNumber(number) {
const stringNumber = number.toString()
const integerDigits = parseFloat(stringNumber.split(".")[0])
const decimalDigits = stringNumber.split(".")[1]
let integerDisplay
if (isNaN(integerDigits)) {
integerDisplay = ""
} else {
integerDisplay = integerDigits.toLocaleString("en", { maximumFractionDigits: 0 })
}
if (decimalDigits != null) {
return `${integerDisplay}.${decimalDigits}`
} else {
return integerDisplay
}
}

updateDisplay() {
this.currentOperandTextElement.innerText = this.getDisplayNumber(this.currentOperand)
if (this.operation != null) {
this.previousOperandTextElement.innerText = `${this.getDisplayNumber(this.previousOperand)} ${this.operation}`
} else {
this.previousOperandTextElement.innerText = ""
}
}
}

const numberButtons = document.querySelectorAll('[data-number]');
const operationButtons = document.querySelectorAll('[data-operation]');
const equalsButton = document.querySelector('[data-equals]');
const deleteButton = document.querySelector('[data-delete]');
const allClearButton = document.querySelector('[data-all-clear]');
const previousOperandTextElement = document.querySelector('[data-previous-operand]');
const currentOperandTextElement = document.querySelector('[data-current-operand]');

const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement)

numberButtons.forEach(button => {
button.addEventListener("click", () => {
calculator.appendNumber(button.innerText)
calculator.updateDisplay()
})
})
operationButtons.forEach(button => {
button.addEventListener("click", () => {
calculator.chooseOperation(button.innerText)
calculator.updateDisplay()
})
})
equalsButton.addEventListener("click", button => {
calculator.compute()
calculator.updateDisplay()
})
allClearButton.addEventListener("click", button => {
calculator.clear()
calculator.updateDisplay()
})
deleteButton.addEventListener("click", button => {
calculator.delete()
calculator.updateDisplay()
})

94 changes: 94 additions & 0 deletions Week1/homework/codeAlong/calculator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
@import url('https://fonts.googleapis.com/css?family=Orbitron&display=swap');
@import url('https://fonts.googleapis.com/css?family=Montserrat&display=swap');


html{height:100%;}

body {
background: rgb(162,159,139);
background: radial-gradient(circle, rgba(162,159,139,1) 0%, rgba(214,212,195,1) 0%, rgba(162,159,139,1) 100%);
}


div.background{
background-color:#55818D;
width: 290px;
min-height:500px;
position: center;
margin-top: 25px;
border-radius: 4%;
padding-top: 10px;
padding-bottom: 10px;
}
div.screen {
width:300px;
min-height: 120px;
max-height: 160px;
background-color: rgba(0,0,0,0.9);
border: 1px solid black;
font-family: 'Orbitron', sans-serif;
letter-spacing:2px;
text-align: right;
display: flex;
align-items: flex-end;
justify-content: space-around;
flex-direction: column;
padding: 10px;
word-wrap: break-word;
word-break: break-all;
overflow: hidden;

}
div.previous-operand {
color: rgba(255,255,255,0.6);
font-size: 1.2rem;
}
div.current-operand {
color: white;
font-size: 2.0rem;
}

button {
width:60px;
height:60px;
background-color: #172027!important;
font-family: 'Montserrat', sans-serif;
font-size: 1.4rem!important;
font-weight: bolder!important;
outline: none;
}
button:hover {
background-color: #283742!important;
border: 1px solid #283742!important;
}
button:active {
background-color: #475d6d!important;
border: 1px solid #475d6d!important;
}

button.equals {
background-color: #fa5f18!important;
border: 1px solid #fa5f18!important;
}

button.equals:hover {
background-color: #f7814a!important;
border: 1px solid #f7814a!important;
}
button.equals:active {
background-color: #f59b72!important;
border: 1px solid #f59b72!important;
}
button.clear {
color: rgb(236, 60, 60);
width: 128px;
}
button.clear:hover{
color: rgb(250, 86, 86);
}
button.clear:focus{
color: rgb(250, 103, 103);
}
button.clear:active {
color: rgb(250, 103, 103);
}
Empty file removed Week1/homework/index.html
Empty file.
9 changes: 9 additions & 0 deletions Week1/homework/js-exercises/animalsArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";
let items = [];
console.log("[]");
console.log(items);
let animals = ["cat", "dog", "rabbit"];
console.log(animals);
animals.push("piglet");
console.log(animals);

7 changes: 7 additions & 0 deletions Week1/homework/js-exercises/compareArrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";
//Variables
let array1 = ["A", "B", "C", "D"];
let array2 = [1, 2, 3, 4, 5, 6, 7];
//Console log
console.log(array1.length);
console.log(array2.length);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good job but you forgot the last question of the exercise: Write a conditional statement that checks if both are of equal length..., thus should also include in your code:

if (array1.length === array2.length) {
    console.log("They are the same!");
} else {
    console.log("Two different sizes");
}

2 changes: 2 additions & 0 deletions Week1/homework/js-exercises/errorDebug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict'
console.log("I'm awesome");
12 changes: 12 additions & 0 deletions Week1/homework/js-exercises/helloWorld.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'
console.log("Hello, world!");//English
console.log("Hola, mundo!");//Spanish
console.log("Ciao, mondo!");//Italian
console.log("Ola, mundo!");//Portuguese
console.log("こんにちは世界!");//Japanese
console.log("Selam Dünya!");//Turkish
console.log("Hallo Wereld!");//Dutch
console.log("Привет, мир!");//Russian
console.log("Hallo Welt!");//German
console.log("Salve mundi!");//Latin(!)

7 changes: 7 additions & 0 deletions Week1/homework/js-exercises/logNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'
let numberX;
console.log("undefined");
console.log(numberX);
numberX = 77;
console.log("The number I assigned to the variable:");
console.log(numberX);
4 changes: 4 additions & 0 deletions Week1/homework/js-exercises/logRemainder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"use strict";
//1. The answer is 1. The number 6 can be divided by 3 so the remainder after the division is 1.
//2. The answer is 1. The number 20 can be divided by 4 so the remainder after the division is 1.
//3. The answer is 1. The number 12 can be divided by 2 so the remainder after the division is 1.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Correct! Here is also the way you can find every remainder (including huge number):

Let have:
5 / 2 = 2.5
Then we say, 2 * 2 = 4
Afterwards, 5 - 4 = 1

So in general is:
a / b = c.d
Then c * b = e,
And a - e = remainder

7 changes: 7 additions & 0 deletions Week1/homework/js-exercises/logString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'
let myString = "Stefanos Leventis";
console.log("My full name");
console.log(myString);
myString = "The Arkitekt";
console.log("My nickname as a music producer");
console.log(myString);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In line 2, the value of the variable myString is "Stefanos Leventis", which happens to be your full name! So, if we want to be 100% precise, the line 3 instead, should be console.log("Stefanos Leventis");

In the line 6, same as above, the new value of the myString is "The Arkitekt", which again, happens to be your nickname as a music producer...

;-D ...yeah yeah I know....

7 changes: 7 additions & 0 deletions Week1/homework/js-exercises/numRound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'
let z = 7.25;
console.log(z);
let a = Math.round(z);
console.log(a);
let x = Math.max(a, z);
console.log(x);
4 changes: 4 additions & 0 deletions Week1/homework/js-exercises/stringLength.js
Loading