Erstellt local mit LM Studio Version 3.23.3 GPT OSS 20 B Hardware Nvidia 2060 Super 8GB Token 8010 CPU AMD 3800X RAM 64 GB | Category | Functions | |----------------|----------------------------------------------------| | Arithmetic | + – × ÷ % √ 1/x | | Trigonometry | sin, cos, tan, asin, acos, atan (all in degrees) | | Logarithms | log10, ln | | Exponentiation | xⁿ, nʸ | | Misc | factorial (!), absolute value, power of ten (10^x) | The UI is deliberately minimal – a grid of buttons and an input field that also doubles as the display. All calculations are performed with JavaScript’s `Math` object; for advanced precision you could swap in **Decimal.js** or similar. --- ## 1 HTML + CSS ```html JavaScript Scientific Calculator
``` --- ## 2️ JavaScript (`calc.js`) ```js // Grab DOM elements const display = document.getElementById('display'); const buttons = document.querySelectorAll('.grid button'); // ---------- Utility helpers ---------- function degToRad(d) { return d * Math.PI / 180; } function radToDeg(r) { return r * 180 / Math.PI; } // Factorial (integer only) function factorial(n){ if (n < 0) throw new Error('Negative factorial'); if (n === 0 || n === 1) return 1; let res = 1; for(let i=2;i<=n;i++) res *= i; return res; } // ---------- Build expression ---------- let expr = ''; // current expression string // Add a token to the expression function appendToken(token){ if (token === 'C') { // clear last entry expr = expr.slice(0, -1); } else if (token === 'AC'){ expr = ''; } else { expr += token; } display.value = expr || '0'; } // ---------- Evaluate ---------- function evaluate(){ try{ // Replace custom functions with Math equivalents let e = expr; // 10**x → 10^x e = e.replace(/10\*\*/g, 'Math.pow(10,'); // x**y → Math.pow(x,y) e = e.replace(/\*\*([\d\.]+)/g, (m,p)=>`),${p})`); // Factorial e = e.replace(/fact\(([^)]+)\)/g,(m,v)=>factorial(eval(v))); // Trig functions (degrees → radians) const trigFuncs = ['sin','cos','tan','asin','acos','atan']; trigFuncs.forEach(f=>{ const re = new RegExp(`${f}\\(([^)]+)\\)`, 'g'); e = e.replace(re,(m,v)=>{ let val = eval(v); if (['sin','cos','tan'].includes(f)){ return `Math.${f}(${degToRad(val)})`; }else{ // inverse trig – output degrees const rad = Math[`${f}`](val); return `${radToDeg(rad)}`; } }); }); // log10 → Math.log10 or Math.log(x)/Math.LN10 e = e.replace(/log10\(([^)]+)\)/g,(m,v)=>`Math.log(${v})/Math.LN10`); // ln → Math.log e = e.replace(/ln\(([^)]+)\)/g,(m,v)=>`Math.log(${v})`); // sqrt → Math.sqrt e = e.replace(/sqrt\(([^)]+)\)/g,(m,v)=>`Math.sqrt(${v})`); // abs → Math.abs e = e.replace(/abs\(([^)]+)\)/g,(m,v)=>`Math.abs(${v})`); // Evaluate the final expression const result = eval(e); display.value = result; expr = '' + result; // allow chaining }catch(err){ display.value = 'Error'; expr = ''; } } // ---------- Event listeners ---------- buttons.forEach(btn=>{ btn.addEventListener('click',()=>{ const val = btn.dataset.value || btn.textContent; if (btn.id === 'equals'){ evaluate(); }else{ appendToken(val); } }); }); ``` ### How it works 1. **UI** – a grid of buttons, each with either `data-value` or an ID (`ac`, `c`, `equals`). 2. **Expression building** – clicking any button appends its token to the string that’s displayed. 3. **Evaluation** – when “=” is pressed, the string is parsed: * Custom functions (`sin(`, `cos(` …) are translated into `Math` calls. * Trigonometric inputs are treated in **degrees**; inverse trig outputs are also converted back to degrees for user‑friendly display. * Factorial is implemented manually because JavaScript has no native operator. 4. The result replaces the current expression so you can continue working. --- ### Quick test Save the two files above (`index.html` and `calc.js`) in the same folder, open `index.html` in a browser and try: ``` AC 7 8 9 × √ = sin(30) cos(60) tan(45) log10(1000) ln(e) fact(5) 10**3 2 ** 8 abs(-42) ``` All should compute correctly, giving you a lightweight but fully‑featured scientific calculator written purely in JavaScript. Happy calculating!