I need the result code below to be displaced in my result box, the code is running without the html interface.. This is exactly what I want to achieve ● 2 inputs to take the 2 elements of the Scale ● A button to calculate the needed weight to balance ● A div to display the result

function pushData() {
  var w1, w2, res;
  var weights = [1, 2, 7, 7];
  w1 = Number(document.scalebalancing.txtnum1.value);
  w2 = Number(document.scalebalancing.txtnum2.value);

  for (var i = 0; i < weights.length; i++) {
    if (w1 + weights[i] === w2 || w2 + weights[i] === w1) {
      return '' + weights[i];
    }
    for (var j = i + 1; j < weights.length; j++) {
      if (w1 + weights[i] + weights[j] === w2 || w2 + weights[i] + weights[j] === w1 || w1 + weights[i] === w2 + weights[j] || w2 + weights[i] === w1 + weights[j]) {
        return '' + weights[i] + ',' + weights[j];

      }
    }
  }
  console.log(pushData());
  res = pushData();
  document.scalebalancing.txtres.value = res;
  return 'Scale Imbalanced';

}
<html>

<head>
  <title>Scale Balancing</title>
</head>

<body>
  <form name="scalebalancing">

    Number 1: <input type="text" name="txtnum1">
    <br> Number 2: <input type="text" name="txtnum2">
    <br> Result : <input type="text" name="txtres"><br>

    <input type="button" value="Calculate" onClick="pushData()">

  </form>


</body>

</html>

3 Answers

0
Tin Tin On Best Solutions

I see that you have return statements in your function which don't let your assignment statement execute at all.

for (var i = 0; i < weights.length; i++) {
  if (w1 + weights[i] === w2 || w2 + weights[i] === w1) { return '' + weights[i]; }
  for (var j = i + 1; j < weights.length; j++) {
    if (w1 + weights[i] + weights[j] === w2 || w2 + weights[i] + weights[j] === w1 || w1 + weights[i] === w2 + weights[j] || w2 + weights[i] === w1 + weights[j]) {
      return '' + weights[i] + ',' + weights[j];
    }
  }
}

Change the return value to assign it to "res" and that would solve the issue for you.

for (var i = 0; i < weights.length; i++) {
  if (w1 + weights[i] === w2 || w2 + weights[i] === w1) { 
    res = '' + weights[i];
  }
  for (var j = i + 1; j < weights.length; j++) {
    if (w1 + weights[i] + weights[j] === w2 || w2 + weights[i] + weights[j] === w1 || w1 + weights[i] === w2 + weights[j] || w2 + weights[i] === w1 + weights[j]) {
      res = weights[i] + ',' + weights[j];
    }
  }
}

Please check the fiddle for the updated code

https://jsfiddle.net/tintin64/gbhdnzqj/10/

0
smurp On

The code example is appearing incompletely. (The top of the form is missing).

Try w1 = Number(document.querySelector('input[name="txtnum1"]').value) as a way to get the value of that input.

0
Rachel Gallen On

First, you need to give your elements ids, this will make you able to reference the values. In the snippet, I assigned the ids as being same as the name, and used document.getElementByID to get the values. Also there is no need for you to call the function a second time in the script (as you are doing where you assign the res value). The function has already been called, so assign the value to res during the script where the calculations occur, otherwise you will get an error. (I got a 'maximum stack' error while composing my answer - thought it was my cache for a minute and went off and cleared it!)

Hope this helps

function pushData() {
  var w1, w2, res;
  var weights = [1, 2, 7, 7];
  w1 = Number(document.getElementById("txtnum1").value);
  w2 = Number(document.getElementById("txtnum2").value);

  for (var i = 0; i < weights.length; i++) {
    if (w1 + weights[i] === w2 || w2 + weights[i] === w1) {
      res =  '' + weights[i];
    }
    for (var j = i + 1; j < weights.length; j++) {
      if (w1 + weights[i] + weights[j] === w2 || w2 + weights[i] + weights[j] === w1 || w1 + weights[i] === w2 + weights[j] || w2 + weights[i] === w1 + weights[j]) {
        res = '' + weights[i] + ',' + weights[j];

      }
    }
  }
  console.log(res);
  //res = pushData();
  document.getElementById("txtres").value = res;
  return 'Scale Imbalanced';

}
<html>

<head>
  <title>Scale Balancing</title>
</head>

<body>
  <form id ="calc" name="scalebalancing">

    Number 1: <input type="text" id="txtnum1" name="txtnum1">
    <br> Number 2: <input type="text" id="txtnum2" name="txtnum2">
    <br> Result : <input type="text" id ="txtres" name="txtres"><br>

    <input type="button" value="Calculate" onClick="pushData()">

  </form>


</body>

</html>