如何成功调用此函数以返回值? JSON.parse() [英] How do I call this function successfully to return a value? JSON.parse()

查看:108
本文介绍了如何成功调用此函数以返回值? 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 sides) and the second element being a list of available weights as positive integers.

例如,如果秤为["[5, 9]", "[1, 2, 6, 7]"],则表示存在一个秤,秤的左侧为5,右侧为9.规模可以像这样2,6

For example, if a scale is ["[5, 9]", "[1, 2, 6, 7]"], then this means there is a balance scale with a weight of 5 on the left side and 9 on the right side. The scale can be balanced like this 2,6  

条件

  1. 秤的第一个元素只能包含2个权重
  2. 可以在秤的一侧添加两个砝码以使其平衡 3.如果无法平衡秤,则您的程序应返回秤不平衡"
  1. The first element of the scale can only contain 2 weights 
  2. It is possible to add two weights to only one side of the scale to balance it  3.If it is not possible to balance the scale then your program should return  "Scale Imbalanced"

我已经能够使用硬编码值对console.log进行操作,并且它可以正常工作.另外,如果没有一个数字可以平衡,则该函数将返回比例不平衡".但是我似乎无法动态调用该函数.

I have been able to do console.log of the function with hardcoded values and it worked. Also, the function returns "scale imbalanced" if none of the numbers can balance. But I can't seem to call the function dynamically.

HTML

...
<input type="text" id="balance" required>
...
<input type="text" id="weights" required>

<div class="button" id="calculateWeight" onclick="balanceIt()"> 
<input type="submit" value="Calculate Weight"></div>
<div id="displayResult"></div>

我想成功调用ScaleBalancing函数以返回一个字符串(如果已调用).当我尝试ScaleBalancing([`${[balance]}`, `${[weights]}`]))时,它返回

I want to successfully call the ScaleBalancing function to return a string if it is called. When I try ScaleBalancing([`${[balance]}`, `${[weights]}`])), it returns

未捕获到的SyntaxError:JSON中位置1处的意外令牌o 在JSON.parse()

Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse ()

JavaScript

JavaScript

function ScaleBalancing(strArr) {
let balance = JSON.parse(strArr[0]);
let weight = JSON.parse(strArr[1]);

const one = balance[0]
const two = balance[1];


for (let i = 0; i < weight.length; i++) {
    let weighted = '' + weight[i];
    if (one + weight[i] === two || two + weight[i] === one) {
        //let weighted = '' + weight[i];
        return weighted;
    }
    for (let j = i + 1; j < weight.length; j++) {

        if (one + weight[i] + weight[j] === two ||
            two + weight[i] + weight[j] === one ||
            one + weight[i] === two + weight[j] ||
            two + weight[i] === one + weight[j]
        ) {
            let balancedScale = '' + weight[i] + ',' + weight[j];;
            return balancedScale;

        }
    }

}

return 'scale imbalanced';
}

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

 document.getElementById("displayResult").innerText = Scale;
  console.log(Scale);
  };

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

推荐答案

例如,如果您输入的是"1,2",则

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

`${[balance]}`

只会导致"1,2",这不是正确的JSON字符串.

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}]`]))

这篇关于如何成功调用此函数以返回值? JSON.parse()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆