Click here to Skip to main content
14,839,837 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi I am new to coding. I WAS WORKING on a project of HTML,CSS and java-script, I am facing a problem in my java-script file while html and CSS files are working well.

What I have tried:

let runningTotal = 0;
let buffer = "0";
let previousOperator;
const screen = document.querySelector(".screen");

function buttonClick(value) {
  if (isNaN(parseInt(value))) {
    handleSymbol(value);
  } else {
    handleNumber(value);
  }
  rerender();
}

function handleNumber(value) {
  if (buffer === "0") {
    buffer = value;
  } else {
    buffer += value;
  }
}

function handleMath(value) {
  if (buffer === "0") {
    // do nothing
    return;
  }

  const intBuffer = parseInt(buffer);
  if (runningTotal === 0) {
    runningTotal = intBuffer;
  } else {
    flushOperation(intBuffer);
  }

  previousOperator = value;

  buffer = "0";
}

function flushOperation(intBuffer) {
  if (previousOperator === "+") {
    runningTotal += intBuffer;
  } else if (previousOperator === "-") {
    runningTotal -= intBuffer;
  } else if (previousOperator === "×") {
    runningTotal *= intBuffer;
  } else {
    runningTotal /= intBuffer;
  }
}

function handleSymbol(value) {
  switch (value) {
    case "C":
      buffer = "0";
      runningTotal = 0;
      break;
    case "=":
      if (previousOperator === null) {
        // need two numbers to do math
        return;
      }
      flushOperation(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 "+":
    case "-":
    case "×":
    case "÷":
      handleMath(value);
      break;
  }
}

function rerender() {
  screen.innerText = buffer;
}

function init() {
  document.querySelector(".calc-buttons").addEventListener("click", function(event) {
    buttonClick(event.target.innerText);
  });
}
alert ("Hey,this is calculator");
Posted
Updated 1-Feb-21 4:30am
Comments
Patrice T 31-Jan-21 14:28pm
   
"I am facing a problem in my java-script file"
And we have to guess what is your problem ?
Richard MacCutchan 31-Jan-21 14:52pm
   
It's a challenge for you. Polish your crystal ball.
Patrice T 31-Jan-21 14:58pm
   
But we are out of mind reading potion. :)
oLiontas 2-Feb-21 2:37am
   
First set javascript script to be at the bottom of the document. Second open from developers tools at your browser the console section to see if you have errors. Then post these errors to us.

1 solution

Javascript has a sometimes-problem that you need to get used to. If you have an error in any file between and then that file will often fail silently. This is typically true even if the block contains separate functions. You need to debug this and see where it goes wrong.

1 - your browser may have a built in debugger (FireFox does).
2 - you need to debug it via "divide and conquer"

The latter is done by commenting out large blocks of the javascript (BUT STILL LEAVING USABLE CODE). If what's left works, look at what's still commented. If it doesn't work, further comment out code until it does and your previous commenting-out block contains the error. Do this as much as is necessary for you to locate the error.

Another version of divide-and-conquer, if you can get any response, is to use a series of alert() in your code. Number them this way: alert(1); alert(2), etc. and see how far you get. The error is, of course, somewhere after your last successful alert(). If it's small, examine it for errors. If it's large, put the earlier alerts that were passed into the broken area, more closely spaced, and see how far you get.

Etc.Etc.Etc. This is actually still an important skill to learn in programming, in general. Certainly if a debugger is not available.
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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