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>
I see that you have return statements in your function which don't let your assignment statement execute at all.
Change the return value to assign it to "res" and that would solve the issue for you.
Please check the fiddle for the updated code
https://jsfiddle.net/tintin64/gbhdnzqj/10/