Click here to Skip to main content
15,434,147 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating a scientific calculator app and currently am unable to make the square root button work. I'm using a switch statement to handle the calculator symbols and not sure exactly how to make this work.

What I have tried:

function handleSymbol(symbol) {
  switch (symbol) {
    case "C":
      buffer = "0";
      runningTotal = 0;
      break;

    case "=":
      if (previousOperator === null) {
        return;
      }
      removeOperation(parseInt(buffer));
      previousOperator = null;
      buffer = runningTotal;
      runningTotal = 0;
      break;

    case "⬅":
      if (buffer.length === 1) {
        buffer = "0";
      } else {
        buffer = buffer.substring(0, buffer.length - 1);
      }
      break;
    case "√":
let sqrt() = Math.sqrt(value);
      break;

    case "+":
    case "-":
    case "*":
    case "/":
    case "%":
      handleMath(symbol);
      break;
  }
}
Posted
Updated 17-Aug-22 0:53am

1 solution

Look at your handleMath method - we can't see it - and look at where it gets it's values from, and what it does with results: You want to do a very similar thing but with just the next value.

But this code is never
let sqrt() = Math.sqrt(value);
going to work: sqrt isn't a method, and let variables go out of scope very quickly and the result would be discarded anyway even if you corrected that: JavaScript Let[^]

To be honest, it looks like you found most of the code you need on the internet and are trying to modify it by guesswork and hope, without any understanding of what the code actually does or how it does it. That isn't going to work: I'd strongly suggest that you try to write your own code rather than try to cheat with someone elses!
 
Share this answer
 
Comments
Emma747 17-Aug-22 7:27am    
The code is part of a bootcamp exercise/project that I wanted to add more functionalities to for practice purposes. I will try to find the solution without using "let". Thanks!
OriginalGriff 17-Aug-22 7:51am    
Seriously, throw the code away, or spend time understanding how it works - you will not find anything useful from the exercise without that!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900