Can anybody help with merging this javascript code to html. Thanks

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>

Generally speaking will tell you to be careful with:

  1. Letter case, things like the ids you specify to your getElementById need to match the case that you specified in the element. Also, getElementByID would not work because of that upper case D.
  2. When you do the push to the array, you are pushing inputText1 although that variable doesn’t exist in your JS, you probably mean w1, as you already have it pulled out?
  3. When creating the if statement, that quote you put in the end, probably a type was messing around.

The general recommendation, open your code in dev tools with Chrome, cant add links still but there are plenty of google official guides or youtube videos with cool guides, would have given you the hints on where the errors are right away :+1:

    <input type = "text" placeholder="Input Weight 1" name="text" id="inputText"/><br> 
<input type = "text" placeholder="Input Weight 2" name="text" id="inputText2"/> <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 = document.getElementById('inputText').value;
	   var w2 = document.getElementById('inputText2').value;
	   
	  
	  // append value to the array
	  myWeights.push(w1); 
	  
	  var pval = "";
	  for(i = 0; i < myWeights.length; i++)
	  {
		pval = pval + myWeights[i];
      }
      document.getElementById('ptext').innerHTML = pval;
	 
 }

</script>

Hope it helps!

1 Like