├── assets ├── calculator.ico ├── MoonIcon.svg └── SunIcon.svg ├── styles ├── light.css └── dark.css ├── index.html └── scripts └── script.js /assets/calculator.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Raushan122002/codsoft-calculator/HEAD/assets/calculator.ico -------------------------------------------------------------------------------- /assets/MoonIcon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /assets/SunIcon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /styles/light.css: -------------------------------------------------------------------------------- 1 | /* CSS Reset & Global Styles */ 2 | * { 3 | box-sizing: border-box; 4 | padding: 0; 5 | margin: 0; 6 | font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Open Sans'; 7 | -webkit-appearance: none; 8 | } 9 | 10 | .wrapper { 11 | height: 100vh; 12 | width: 100%; 13 | display: flex; 14 | justify-content: center; 15 | flex-direction: column; 16 | align-items: center; 17 | background-color: rgb(7, 210, 199); 18 | transition: 0.8s all; 19 | } 20 | 21 | h1 { 22 | margin-bottom: 1.5%; 23 | color: #fff; 24 | font-weight: normal; 25 | } 26 | 27 | .container { 28 | width: 350px; 29 | display: flex; 30 | justify-content: center; 31 | flex-direction: column; 32 | align-items: center; 33 | } 34 | 35 | .header-container { 36 | display: flex; 37 | justify-content: space-between; 38 | align-items: center; 39 | width: 80%; 40 | } 41 | 42 | .top-buttons { 43 | display: flex; 44 | align-items: center; 45 | } 46 | 47 | input { 48 | padding: 25px; 49 | color: rgb(87, 87, 87); 50 | font-size: 1em; 51 | cursor: pointer; 52 | width: 70px; 53 | background-color: #fff; 54 | border: none; 55 | outline: none; 56 | border-radius: 100px; 57 | margin: 0.2em; 58 | } 59 | 60 | .first-row, 61 | .second-row, 62 | .third-row, 63 | .fourth-row, 64 | .fifth-row { 65 | margin-bottom: 4px; 66 | } 67 | 68 | input[type="button"] { 69 | color: rgb(122, 122, 122); 70 | } 71 | 72 | input[type="text"] { 73 | background-color: rgb(255, 255, 255); 74 | width: 222.5px; 75 | } 76 | 77 | input[type="button"]:hover { 78 | background-color: rgb(37, 35, 59); 79 | color: #fff; 80 | } 81 | 82 | #clear-button { 83 | color: #fff; 84 | background-color: rgb(255, 25, 25); 85 | } 86 | 87 | a { 88 | text-decoration: none; 89 | color: #fff; 90 | } 91 | 92 | #github-icon { 93 | margin-right: 10px; 94 | } 95 | 96 | .theme-button { 97 | all: unset; 98 | cursor: pointer; 99 | } 100 | .author-text{ 101 | color: white; 102 | margin-top: 20px; 103 | } 104 | -------------------------------------------------------------------------------- /styles/dark.css: -------------------------------------------------------------------------------- 1 | /* CSS Reset & Global Styles */ 2 | * { 3 | box-sizing: border-box; 4 | padding: 0; 5 | margin: 0; 6 | font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Open Sans'; 7 | -webkit-appearance: none; 8 | } 9 | 10 | ::placeholder { 11 | color: rgb(221, 221, 221); 12 | } 13 | 14 | .wrapper { 15 | height: 100vh; 16 | width: 100%; 17 | display: flex; 18 | justify-content: center; 19 | flex-direction: column; 20 | align-items: center; 21 | background-color: rgb(20, 19, 19); 22 | transition: 0.8s all; 23 | } 24 | 25 | h1 { 26 | margin-bottom: 1.5%; 27 | color: #fff; 28 | font-weight: normal; 29 | } 30 | 31 | .container { 32 | width: 350px; 33 | display: flex; 34 | justify-content: center; 35 | flex-direction: column; 36 | align-items: center; 37 | } 38 | 39 | .header-container { 40 | display: flex; 41 | justify-content: space-between; 42 | align-items: center; 43 | width: 80%; 44 | } 45 | 46 | .top-buttons { 47 | display: flex; 48 | align-items: center; 49 | 50 | } 51 | 52 | input { 53 | padding: 25px; 54 | color: rgb(255, 255, 255); 55 | font-size: 1em; 56 | cursor: pointer; 57 | width: 70px; 58 | background-color: rgb(47, 51, 50); 59 | border: none; 60 | outline: none; 61 | border-radius: 100px; 62 | margin: 0.2em; 63 | } 64 | 65 | .first-row, 66 | .second-row, 67 | .third-row, 68 | .fourth-row, 69 | .fifth-row { 70 | margin-bottom: 4px; 71 | } 72 | 73 | input[type="text"] { 74 | background-color: rgb(47, 51, 50); 75 | width: 222.5px; 76 | } 77 | 78 | input[type="button"]:hover { 79 | background-color: rgb(101, 101, 101); 80 | color: #fff; 81 | } 82 | 83 | #clear-button { 84 | color: #fff; 85 | background-color: rgb(255, 42, 42); 86 | } 87 | 88 | a { 89 | text-decoration: none; 90 | color: #fff; 91 | } 92 | 93 | #github-icon { 94 | margin-right: 10px; 95 | } 96 | 97 | .theme-button { 98 | all: unset; 99 | cursor: pointer; 100 | text-align: center; 101 | } 102 | 103 | 104 | .author-text{ 105 | color: white; 106 | margin-top: 20px; 107 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Calculator 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 |
20 |
21 | 22 |

Calculator

23 |
24 | 27 |
28 |
29 |
30 | 31 | 32 |
33 |
34 | 35 | 36 | 37 | 38 |
39 |
40 | 41 | 42 | 43 | 44 |
45 |
46 | 47 | 48 | 49 | 50 |
51 |
52 | 53 | 54 | 55 | 56 |
57 |
58 |
59 |

Crafted with passion and dedication by

60 | Raushan Kr.❤️ 61 |
62 |
63 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /scripts/script.js: -------------------------------------------------------------------------------- 1 | const lightTheme = "styles/light.css"; 2 | const darkTheme = "styles/dark.css"; 3 | const sunIcon = "assets/SunIcon.svg"; 4 | const moonIcon = "assets/MoonIcon.svg"; 5 | const themeIcon = document.getElementById("theme-icon"); 6 | const res = document.getElementById("result"); 7 | const toast = document.getElementById("toast"); 8 | 9 | function calculate(value) { 10 | const calculatedValue = eval(value || null); 11 | if (isNaN(calculatedValue)) { 12 | res.value = "Can't divide 0 with 0"; 13 | setTimeout(() => { 14 | res.value = ""; 15 | }, 1300); 16 | } else { 17 | res.value = calculatedValue; 18 | } 19 | } 20 | 21 | // Swaps the stylesheet to achieve dark mode. 22 | function changeTheme() { 23 | const theme = document.getElementById("theme"); 24 | setTimeout(() => { 25 | toast.innerHTML = "Calculator"; 26 | }, 1500); 27 | if (theme.getAttribute("href") === lightTheme) { 28 | theme.setAttribute("href", darkTheme); 29 | themeIcon.setAttribute("src", sunIcon); 30 | toast.innerHTML = "Dark Mode 🌙"; 31 | } else { 32 | theme.setAttribute("href", lightTheme); 33 | themeIcon.setAttribute("src", moonIcon); 34 | toast.innerHTML = "Light Mode ☀️"; 35 | } 36 | } 37 | 38 | // Displays entered value on screen. 39 | function liveScreen(enteredValue) { 40 | if (!res.value) { 41 | res.value = ""; 42 | } 43 | res.value += enteredValue; 44 | } 45 | 46 | //adding event handler on the document to handle keyboard inputs 47 | document.addEventListener("keydown", keyboardInputHandler); 48 | 49 | //function to handle keyboard inputs 50 | function keyboardInputHandler(e) { 51 | // to fix the default behavior of browser, 52 | // enter and backspace were causing undesired behavior when some key was already in focus. 53 | e.preventDefault(); 54 | //grabbing the liveScreen 55 | 56 | //numbers 57 | if (e.key === "0") { 58 | res.value += "0"; 59 | } else if (e.key === "1") { 60 | res.value += "1"; 61 | } else if (e.key === "2") { 62 | res.value += "2"; 63 | } else if (e.key === "3") { 64 | res.value += "3"; 65 | } else if (e.key === "4") { 66 | res.value += "4"; 67 | } else if (e.key === "5") { 68 | res.value += "5"; 69 | } else if (e.key === "6") { 70 | res.value += "6"; 71 | } else if (e.key === "7") { 72 | res.value += "7"; 73 | } else if (e.key === "7") { 74 | res.value += "7"; 75 | } else if (e.key === "8") { 76 | res.value += "8"; 77 | } else if (e.key === "9") { 78 | res.value += "9"; 79 | } 80 | 81 | //operators 82 | if (e.key === "+") { 83 | res.value += "+"; 84 | } else if (e.key === "-") { 85 | res.value += "-"; 86 | } else if (e.key === "*") { 87 | res.value += "*"; 88 | } else if (e.key === "/") { 89 | res.value += "/"; 90 | } 91 | 92 | //decimal key 93 | if (e.key === ".") { 94 | res.value += "."; 95 | } 96 | 97 | //press enter to see result 98 | if (e.key === "Enter") { 99 | calculate(result.value); 100 | } 101 | 102 | //backspace for removing the last input 103 | if (e.key === "Backspace") { 104 | const resultInput = res.value; 105 | //remove the last element in the string 106 | res.value = resultInput.substring(0, res.value.length - 1); 107 | } 108 | } 109 | --------------------------------------------------------------------------------