-
Notifications
You must be signed in to change notification settings - Fork 9
Some changes on exercise 8. #9
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
base: master
Are you sure you want to change the base?
Changes from all commits
047d357
c39774e
3c2f67a
4423f93
c05cf61
c1d9588
7270f62
66a7872
3c51e93
c63dd12
6bb8168
067af8e
b6266fa
3ff4d94
226bcf3
23ad693
7cc54ba
53a596d
e3340c4
cb8867f
a1e06f2
75aeb9d
97ef941
ebec922
1e424c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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> |
| 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() | ||
| }) | ||
|
|
| 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); | ||
| } |
| 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); | ||
|
|
| 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); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 'use strict' | ||
| console.log("I'm awesome"); |
| 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(!) | ||
|
|
| 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); |
| 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. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): |
||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In line 2, the value of the variable In the line 6, same as above, the new value of the ;-D ...yeah yeah I know.... |
||
| 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); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| "use strict"; | ||
| let mySentence = "Programming is so interesting!"; | ||
| let howLongIsIt = mySentence.length; | ||
| console.log(howLongIsIt); |
There was a problem hiding this comment.
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: