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

Mehdi branch #146

Open
wants to merge 5 commits into
base: main
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
12 changes: 5 additions & 7 deletions react-calculator/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
import React from 'react';
import './App.css';
import Layout from './components/Layout';
import Buttons from './components/Buttons';
import { useState } from 'react';
import Display from './components/Display';
import { ButtonValue } from "./components/Buttons";
import { handleButtonLogic } from './components/CalculatorLogic';
import { useEffect } from 'react'
import { useEffect } from 'react';

function App() {



const getButtonInput = (value: ButtonValue) => {
console.log('getButtonInput triggered with value:', value);
const updatedDisplay = handleButtonLogic(displayValue, value);
console.log('Updated display:', updatedDisplay);
setDisplayValue(updatedDisplay);
}
}

// State for button selected
const [displayValue, setDisplayValue] = useState<string[]>([]);

useEffect(() => {
console.log("displayValue has been updated:", displayValue);
}, [displayValue]);
}, [displayValue]);

return (
<div className="App">
<h1>React Calculator: Made with TypeScript</h1>
<Layout>
<Display value={displayValue.join(' ')} />
<Buttons getButtonInput={getButtonInput} />
</Layout >
</Layout>
</div>
);
}
Expand Down
151 changes: 101 additions & 50 deletions react-calculator/src/components/ButtonLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,105 @@ This function handles the math / logic associated with each button
Due to the large nature of this function, I moved it to a separate file so App.tsx is not so congested
*/

import { ButtonValue } from "./Buttons";
// ButtonLogic.tsx
// ButtonLogic.tsx
import { ButtonValue as ImportedButtonValue } from "./Buttons";

export const handleButtonClick = (button: ButtonValue):string => {
console.log("HandleButtonClick function firing")
switch (button) {
case "AC":
return "AC"
case "()":
return "()"
case "%":
return "%"
case "/":
return "/"
case "7":
return "7";
case "8":
return "8"
case "9":
return "9"
case "X":
return "X"
case "4":
return "4"
case "5":
return "5"
case "6":
return "6"
case "-":
return "-"
case "1":
return "1"
case "2":
return "2"
case "3":
return "3"
case "+":
return "+"
case "0":
return "0"
case ".":
return "."
case "<-- Back":
return "back"
case "=":
return "="
default:
// Edge casing
console.warn("Unhandled button value:", button);
return "Error"
}
}
export enum ButtonValue {
AC = "AC",
Parentheses = "()",
Percent = "%",
Divide = "/",
Seven = "7",
Eight = "8",
Nine = "9",
Multiply = "X",
Four = "4",
Five = "5",
Six = "6",
Minus = "-",
One = "1",
Two = "2",
Three = "3",
Plus = "+",
Zero = "0",
Decimal = ".",
Back = "<-- Back",
Equals = "=",
Sin = "sin",
Cos = "cos",
Tan = "tan",
Log = "log",
Ln = "ln",
Sqrt = "sqrt",
E = "e",
Pi = "π",
}

export const handleButtonClick = (button: ButtonValue): string => {
switch (button) {
case ButtonValue.AC:
return "AC";
case ButtonValue.Parentheses:
return "()";
case ButtonValue.Percent:
return "%";
case ButtonValue.Divide:
return "/";
case ButtonValue.Seven:
return "7";
case ButtonValue.Eight:
return "8";
case ButtonValue.Nine:
return "9";
case ButtonValue.Multiply:
return "X";
case ButtonValue.Four:
return "4";
case ButtonValue.Five:
return "5";
case ButtonValue.Six:
return "6";
case ButtonValue.Minus:
return "-";
case ButtonValue.One:
return "1";
case ButtonValue.Two:
return "2";
case ButtonValue.Three:
return "3";
case ButtonValue.Plus:
return "+";
case ButtonValue.Zero:
return "0";
case ButtonValue.Decimal:
return ".";
case ButtonValue.Back:
return "back";
case ButtonValue.Equals:
return "=";

// Scientific Functions and Constants
case ButtonValue.Sin:
return "sin(";
case ButtonValue.Cos:
return "cos(";
case ButtonValue.Tan:
return "tan(";
case ButtonValue.Log:
return "log(";
case ButtonValue.Ln:
return "ln(";
case ButtonValue.Sqrt:
return "sqrt(";
case ButtonValue.E:
return "e";
case ButtonValue.Pi:
return "π";

default:
// Edge casing
console.warn("Unhandled button value:", button);
return "Error";
}
};
120 changes: 70 additions & 50 deletions react-calculator/src/components/Buttons.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,77 @@
import React from 'react'
import React from 'react';

export type ButtonValue =
| "AC"
| "()" // Parentheses
| "%"
| "/"
| "7" | "8" | "9" | "X" // Updated for "X" as multiplication
| "4" | "5" | "6" | "-"
| "1" | "2" | "3" | "+"
| "0" | "."
| "<-- Back"
| "="
| "sin" | "cos" | "tan" // Trigonometric functions
| "π" | "e" // Constants
| "x^y" | "x^(1/n)" // Exponents and nth root
| "log" | "ln" // Logarithmic functions
| "n!" // Factorial
| "MR" | "MC" | "M+" | "M-" // Memory functions
| "xPn" | "nCr"; // Advanced operations

/*
* This component contains the code for all the buttons.

I understand that you can create the buttons with mapping through a 'buttons' object, but until we get this running I want to keep each individual button for easier styling and testing purposes.
*/

// Interface for the specific type of values that can be passed into the button props
export type ButtonValue = "AC" | "()" | "%" | "/" | "7" | "8" | "9" | "X" | "4" | "5" | "6" | "-" | "1" | "2" | "3" | "+" | "0" | "." | "<-- Back" | "=";

// Interface for the function that is passed down to the
export type ButtonProps = {
getButtonInput: (value: ButtonValue) => void;
}
getButtonInput: (value: ButtonValue) => void;
};

const Buttons = ({getButtonInput}: ButtonProps) => {
const Buttons = ({ getButtonInput }: ButtonProps) => {
return (
<div className="buttons-container">
{/* Top Row */}
<button className="AC-button" value="AC" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>AC</button>
<button value="/" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>/</button>



{/* Second Row */}
<button value="7" onClick={(e) => { console.log('Button clicked:', e.currentTarget.value); getButtonInput(e.currentTarget.value as ButtonValue);}}>7</button>
<button value="8" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>8</button>
<button value="9" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>9</button>
<button value="*" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>X</button>



{/* Third Row */}
<button value="4" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>4</button>
<button value="5" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>5</button>
<button value="6" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>6</button>
<button value="-" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>-</button>

{/* Fourth Row */}
<button value="1" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>1</button>
<button value="2" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>2</button>
<button value="3" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>3</button>
<button value="+" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>+</button>

{/* Fifth Row */}
<button value="0" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>0</button>
<button value="." onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>.</button>
<button value="<-- Back" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>{"<-- Back"}</button>
<button className="equal-button" value="=" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>=</button>


{/* Top Row */}
<button className="AC-button" value="AC" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>AC</button>
<button value="/" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>/</button>
<button value="7" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>7</button>
<button value="8" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>8</button>
<button value="9" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>9</button>
<button value="X" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>X</button>

{/* Second Row */}
<button value="4" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>4</button>
<button value="5" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>5</button>
<button value="6" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>6</button>
<button value="-" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>-</button>

{/* Third Row */}
<button value="1" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>1</button>
<button value="2" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>2</button>
<button value="3" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>3</button>
<button value="+" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>+</button>

{/* Fourth Row */}
<button value="0" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>0</button>
<button value="." onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>.</button>
<button value="<-- Back" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>{"<-- Back"}</button>
<button className="equal-button" value="=" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>=</button>

{/* New Buttons: Trigonometric Functions, Constants, and More */}
<button value="sin" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>sin</button>
<button value="cos" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>cos</button>
<button value="tan" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>tan</button>
<button value="π" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>π</button>
<button value="e" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>e</button>
<button value="x^y" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>x^y</button>
<button value="x^(1/n)" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>x^(1/n)</button>
<button value="log" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>log</button>
<button value="ln" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>ln</button>
<button value="n!" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>n!</button>
<button value="MR" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>MR</button>
<button value="MC" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>MC</button>
<button value="M+" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>M+</button>
<button value="M-" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>M-</button>
<button value="xPn" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>xPn</button>
<button value="nCr" onClick={(e) => getButtonInput(e.currentTarget.value as ButtonValue)}>nCr</button>
</div>
)
}
);
};

export default Buttons;

export default Buttons
Loading