-1

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>

1
  • please add the input and wanted result. Apr 27, 2019 at 21:17

3 Answers 3

0

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
0

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.

1
  • not working, I have edited the code now, the complete code is now showing Apr 27, 2019 at 19:50
0

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>

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.