DEV Community

Mohamed Hashir
Mohamed Hashir

Posted on

Please help me with my Javascript

This is my calculator and I have put my highest effort in CSS, HTML and JS. But my JS code is'nt working, please help me with it
function getSelectedItem() {
const selectElement = document.getElementById('symbol');
const selectedIndex = selectElement.selectedIndex;
const selectedItem = selectElement.options[selectedIndex].value;
return selectedItem;
}
function c() {
const n1 = parseFloat(document.getElementById('n1').value);
const n2 = parseFloat(document.getElementById('n2').value);
var symbol = getSelectedItem();
const n3 = document.getElementById('n3').value;

if (symbol === 'add') {
n3 = n1 + n2;
} else if (symbol === 'sub') {
n3 = n1 - n2;
} else if (symbol === 'div') {
n3 = n1 / n2;
} else if (symbol === 'mul') {
n3 = n1 * n2;
}

document.getElementById('n3').value = n3;
}

Top comments (7)

Collapse
 
urickalberth profile image
UrickAlberth

I see that you've put effort into creating a calculator with HTML, CSS, and JavaScript. Let's take a look at your code and identify the issues:

  1. The variable n3 is declared as a constant (const) and then reassigned in the c() function, which is not allowed. Instead, you can declare it as a regular variable using let since its value will change.

  2. You are using the same variable n3 for both getting its value (const n3 = document.getElementById('n3').value;) and storing the result. It's better to use a different variable for storing the result.

Let's make these adjustments to your code:

<!-- Your HTML code (assuming you have appropriate input fields and select dropdown) -->

<select id="symbol">
  <option value="add">Add</option>
  <option value="sub">Subtract</option>
  <option value="div">Divide</option>
  <option value="mul">Multiply</option>
</select>

<input type="number" id="n1">
<input type="number" id="n2">
<button onclick="c()">Calculate</button>
<input type="text" id="result" readonly>
Enter fullscreen mode Exit fullscreen mode
// JavaScript code with adjustments

function getSelectedItem() {
  const selectElement = document.getElementById('symbol');
  const selectedIndex = selectElement.selectedIndex;
  const selectedItem = selectElement.options[selectedIndex].value;
  return selectedItem;
}

function c() {
  const n1 = parseFloat(document.getElementById('n1').value);
  const n2 = parseFloat(document.getElementById('n2').value);
  const symbol = getSelectedItem();
  let result;

  if (symbol === 'add') {
    result = n1 + n2;
  } else if (symbol === 'sub') {
    result = n1 - n2;
  } else if (symbol === 'div') {
    result = n1 / n2;
  } else if (symbol === 'mul') {
    result = n1 * n2;
  }

  document.getElementById('result').value = result;
}
Enter fullscreen mode Exit fullscreen mode

With these changes, your calculator should work as expected. When you click the "Calculate" button, it will take the values of n1 and n2, perform the selected operation based on the dropdown (symbol), and display the result in the result input field.

Collapse
 
tristan_m profile image
Tristan M・トリスタン

Thanks chat gpt?

Collapse
 
m_hashir147 profile image
Mohamed Hashir

Thank you so much.

Collapse
 
vulcanwm profile image
Medea

hey can you provide a link to your whole code via Replit, CodePen or CodeSandbox?

Collapse
 
m_hashir147 profile image
Mohamed Hashir

k , but my calculator is still not working

Collapse
 
vulcanwm profile image
Medea

ah okay, so the bug is that you've defined n3 as a constant, but then you try to change it's value.
to fix it, instead of defining it as a constant, define it as a variable

Collapse
 
m_hashir147 profile image
Mohamed Hashir