I have this code, I use console. log to add input to it.
but now I want it to take Input from textbox, please how do I go about it…
I want my weights to be constants…
like weights = [1,2,3,5]; always… that should not be an input…
function ScaleBalancing(strArr) {
var w1 = JSON.parse(strArr[0])[0];
var w2 = JSON.parse(strArr[0])[1];
var weights = JSON.parse(strArr[1]);
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];
}
}
}
return ‘Scale Imbalanced’;
}
console.log(ScaleBalancing(["[3, 5]", “[1, 2, 7, 7]”])); // 1
console.log(ScaleBalancing(["[13, 4]", “[1, 2, 3, 6, 14]”])); // 3,6
This is the html I am working on… feel free to scrap it, if you have a better solution… thanks
Scale Balancing
Submitted by Folalu Timothy
<input type = "text" placeholder="Input Weight 1" name="text" id="inputText"/><br>
<input type = "text" placeholder="Input Weight 2" name="text" id="inputTextw"/> <br>
<br>
<button onclick ="addWeight();">Calculate</button>
<p id="ptext"></p>
<script>
var myWeights = ["a","b","c","d"];
function addWeight()
{
// get value from the input text
var w1 = documment.getElementById('inputText').value;
var w2 = document.getElementById('inputText2').value;
// append value to the array
myWeights.push(inputText1);
var pval = "";
for(i = 0; i < myWeights.length; i++")
{
pval = pval + myWeights[i];
}
document.getElementByID('pText').innerHTML = pval;
}
</script>