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

Develop ronald rios frontend #73

Open
wants to merge 3 commits 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
17 changes: 17 additions & 0 deletions Front-End/challenge1/script.js
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
// Modify this file only

const btnIncrease = document.getElementById("increase");
const btnDecrease = document.getElementById("decrease");
const spanCounter = document.getElementById("counter");

function getCurrentCount() {
return Number(spanCounter.innerText);
}

btnIncrease.addEventListener("click", () => {
spanCounter.innerText = getCurrentCount() + 1;
});


btnDecrease.addEventListener("click", () => {
spanCounter.innerText = getCurrentCount() - 1;
});
2 changes: 1 addition & 1 deletion Front-End/challenge2/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
</div>
</div>

<script src="./script"></script>
<script src="./script.js"></script>
</body>
</html>
190 changes: 190 additions & 0 deletions Front-End/challenge2/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,193 @@ TO-DO:
- The calculator should be completely functional

*/

const SUBTRACK = "subtrack";
const MULTIPLICATION = "multiplication";
const DIVISION = "division";
const ADD = "add";
const EQUALS = "equals";

const operatorNames = [SUBTRACK, MULTIPLICATION, DIVISION, ADD, EQUALS];

const keyboardKeysOperators = {
"-": SUBTRACK,
"*": MULTIPLICATION,
"/": DIVISION,
"+": ADD,
Enter: EQUALS,
};

// OPERATORS
let operatorFunctions = {
subtrack: (a, b) => a - b,
multiplication: (a, b) => a * b,
division: (a, b) => {
if (b === 0) return "Divide by zero now allowed, re-enter number";
return a / b;
},
add: (a, b) => a + b,
equals: (a, b) => a,
};

const numberNames = [
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
];

///Main functionality
let data = {
lastValue: "",
lastOperator: "",
currentValue: "",
lastKeyPressed: "",
};

function printValues() {
// console.log(data); // Uncomment for testing purposes only
}
function resetData() {
data = {
lastValue: "",
lastOperator: "",
currentValue: "",
lastKeyPressed: "",
};
}

//Display results on UI
const elementDisplay = document.getElementById("display");
function updateDisplay(val) {
elementDisplay.innerText = val;
}

// KEYBOARD ENTRIES
addEventListener("keypress", (event) => {
if (event.key.toLowerCase() == "c") {
//delete using c
resetData();
updateDisplay(data.currentValue);

printValues();
} else if (!isNaN(event.key)) {
numberPressed(event.key);
} else if (keyboardKeysOperators[event.key]) {
operationPressed(keyboardKeysOperators[event.key]);
}
});

function numberPressed(number) {
if (data.lastOperator === EQUALS) {
data.currentValue = "";
data.lastOperator = "";
}
data.currentValue = data.currentValue + number;
elementDisplay.innerText = data.currentValue;

data.lastKeyPressed = number;
printValues();
}

function operationPressed(currentOperatorPressed) {
printValues();

///the equals operator serves as clear after returning operation
if (currentOperatorPressed === EQUALS && data.lastOperator === EQUALS) {
resetData();
updateDisplay("");
return;
}

///Handle 1st number as negative
let isSignChange = false;
if (currentOperatorPressed === SUBTRACK) {
if (data.lastOperator === EQUALS) {
data.currentValue = "-";
isSignChange = true;
data.lastOperator = "";
} else if (data.currentValue === "-") {
data.currentValue = "";
isSignChange = true;
} else if (data.currentValue === "") {
data.currentValue = "-";
isSignChange = true;
}

updateDisplay(data.currentValue);
}

if (isSignChange) {
data.lastKeyPressed = currentOperatorPressed;
return;
}

//HANDLER OPERATOR CLICKED IN A ROW: update operator only unless it
if (
operatorNames.includes(data.lastKeyPressed) &&
data.lastOperator !== EQUALS
) {
data.lastOperator = currentOperatorPressed;
data.lastKeyPressed = currentOperatorPressed;
return;
}

calculateResult(currentOperatorPressed);
data.lastKeyPressed = currentOperatorPressed;
printValues();
}

function calculateResult(nextOperator) {
let result;
if (data.lastOperator === EQUALS) {
data.lastOperator = ""; //avoid execute an operation
}

// if (lastOperator && lastValue && currentValue) {
if (data.lastOperator && data.currentValue) {
result = operatorFunctions[data.lastOperator](
Number(data.lastValue),
Number(data.currentValue)
);

if (isNaN(result)) {
alert("Can't divide by zero!"); // division by zero
return;
}

updateDisplay(result ?? "");
if (nextOperator === EQUALS) {
updateDisplay("=\t\t\t\t " + result);
}

if (data.lastOperator === EQUALS) {
data.currentValue = data.lastValue; //for chaining operations with the answer after EQUALS
} else {
data.currentValue = "";
}
}

data.lastValue = result ?? data.currentValue;
data.currentValue = nextOperator == EQUALS ? data.lastValue : "";
data.lastOperator = nextOperator;
}

// LISTENNERS on UI Buttons clicked
numberNames.forEach((id, index) => {
document.getElementById(id).addEventListener("click", () => {
numberPressed(index);
});
});

operatorNames.forEach((idOperator) =>
document.getElementById(idOperator).addEventListener("click", () => {
operationPressed(idOperator);
})
);