0

My brain hurts. I cant manage to get the function to update a different variable depending on what the parameter says the variable is. I have been looking at this for a while and I am not that good at JavaScript. Can anyone help me? Also not sure if i have forgotten anything.

<input type="number" value="0" id="input2"/>
<button id="plus2" onclick="runPlus('input2', total2, 3)">+</button>
<input type="number" value="0" id="input2"/>
<button id="plus2" onclick="runPlus('input2', total2, 3)">+</button>
<p>$<span id="total"></span></p>

<script>
const total = document.getElementById('total');
var total1 = 0;
var total2 = 0;
function getId(id) {
    return document.getElementById(id);
};
function runPlus(inputNum, totalNum, worth) {
  const currentValue = Number(getId(inputNum).value) || 0;
  getId(inputNum).value = Math.round(currentValue) + 1;
  totalNum = Number(getId(inputNum).value) * worth; //This line is not working
  refresh();
};
function refresh() {
    return total.textContent = total1 + total2;
};
</script>
  • When you pass `total2` or `total1` into `runPlus` as the `totalNum` parameter, you are passing the **value** only. Assigning a new value to `totalNum` only updates `totalNum` which is only scoped within your `runPlus` function – Phil Jun 24 '21 at 02:37
  • Check out this fiddle, it might help you :) (https://jsfiddle.net/4q8jsmtf/) – yogski Jun 24 '21 at 03:50

0 Answers0