Skip to content Skip to sidebar Skip to footer

How Do I Call This Function Successfully To Return A Value? Json.parse()

I am trying to solve this code challenge on scale balancing. A Scale that contains two elements, the first being the two positive integer weights on a balance scale (left and right

Solution 1:

it didn't work for my either. what I did is define it from the javascript so I knew for sure that the method is defined before calling it.

I believe what happened is that the on click method is not on the same scope as the balanceIt method and that is why it's not recognised.

hope it helped you

var balanceIt = function() {
  let weights = document.getElementById("weights").value;
  let balance = document.getElementById("balance").value;
  let scale = ScaleBalancing([`${[balance]}`, `${[weights]}`]);

  document.getElementById("displayResult").innerText = scale;
};

document.getElementById("calculateWeight").onclick = balanceIt;

Solution 2:

If your input is for example "1,2", then

`${[balance]}`

would result in just "1,2" which is not a proper JSON string.

What you want instead is probably (notice the different square bracket positions) :

ScaleBalancing([`[${balance}]`, `[${weights}]`]))

Post a Comment for "How Do I Call This Function Successfully To Return A Value? Json.parse()"