首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >属性在从另一个数组获取值时返回错误的值。

属性在从另一个数组获取值时返回错误的值。
EN

Stack Overflow用户
提问于 2022-07-19 16:30:18
回答 1查看 31关注 0票数 0

我在为在线课程“Odin项目”做一个项目,我在做计算器项目。我试图得到一个简单方程的列表,这些方程来自较长的方程,来自用户的输入,然后将方程按第一个数、操作符和第二个数分开。问题是,第一个数字的值是返回实际值,添加到一个看似随机的值中。下面是我的代码示例:

代码语言:javascript
复制
function operate(){
//Declare an array that is equal to the return value of getEquation()
let equations = getEquation();
console.log(equations);
//For each item in the equations array, check what the operator is and if it's '*' or '/', call the appropriate function on the numbers in the array. Make the outputValue the answer, and make num1 of the next equation the answer of the previous equation
let answer = null;
for(let i=0; i<equations.length; i++){
    //If the previous answer isn't null, make that the value of num1
    if(answer != null){
        equations[i].num1 = answer;
    }
    switch(equations[i].operator){
        case '*': answer = multiply(equations[i].num1, equations[i].num2)
        console.log(answer);
        finalizeEquation(answer);
        break;
        case '/': answer = divide(equations[i].num1, equations[i].num2)
        finalizeEquation()
        break;
    }
}
//Then, do the same thing again but check if the operator is '+' or '-'
for(let i=0; i<equations.length; i++){
    //If the previous answer isn't null, make that the value of num1
    if(answer != null){
        equations[i].num1 = answer;
    }
    switch(equations[i].operator){
        case '+': answer = add(equations[i].num1, equations[i].num2)
        finalizeEquation()
        break;
        case '/': answer = subtract(equations[i].num1, equations[i].num2)
        finalizeEquation()
        break;
    }
}
answer = null;

//Declare getEquation(), which returns an array of objects with operator, num1, and num2 as properties
function getEquation(){
    console.log(inputArray)
    //Declare equations array
    const equations = [];
    for(let i=0; i<inputArray.length; i++){
        //Check if the current index in the array is an operator
        if(isOperator(inputArray[i])){
            console.log(inputArray[i-1]);
            //Push an object with the number before the operator as "num1", the operator as "operator", and the number after the operator as "num2"
            equations.push({
                num1: parseFloat(inputArray[i-1]),
                operator: inputArray[i],
                num2: parseFloat(inputArray[i+1])
            })
        }
    }
    //Return the equations array
    return equations;
}

Console.log(方程)应该给我显示方程的值。我一直在用方程式"1 +3* 6“来测试这一点,而方程组数组返回的是完全不同的东西。下面是一个用上述公式测试的示例:登录到控制台的消息

消息返回数组{…},{…}num1 0:对象{ num1: 18,运算符:"+",num2: 3}​1:对象{ num1: 21,运算符:"",num2: 6}​长度: 2*

我还在传递之前将值传递给console.logged,它返回了正确的值,所以我认为它一定是对象的问题,但是我已经找了好几个小时了,找不到它。我是初学者,所以请对我放松点!谢谢!

EN

回答 1

Stack Overflow用户

发布于 2022-07-19 16:54:36

在查看了代码库之后,我发现了为什么您会遇到这个问题。让我们看看您的输出和代码。

首先,得到方程为[{ num1: 1, operator: "+", num2: 3 }, {num1: 3, operator: "*", num2: 6 }​],答案为null。

现在,在第一个for循环中,由于应答为null,所以不会更新任何等式。此外,您将执行乘法运算并在应答中存储值18 (3 * 6)。

所以,在第一个循环之后,我们有方程作为[{ num1: 1, operator: "+", num2: 3 }, {num1: 3, operator: "*", num2: 6 }​],而答案是18。

现在,在第二个循环中,我们有非空答案值(即18)。因此,您将这个答案值赋值给第一个方程的num1,因此{ num1: 1, operator: "+", num2: 3 }对象值被更新为{ num1: 18, operator: "+", num2: 3 }。另外,您将执行加法操作,并将其存储在应答变量中。

但是,由于数组中有更多的元素,我们将转到下一个元素,即{num1: 3, operator: "*", num2: 6 }。由于答案是非空值(21),这个对象的num1将被更新,该对象将变成{num1: 21, operator: "*", num2: 6 }

因此,在执行该函数之后,您将得到[{ num1: 18, operator: "+", num2: 3 }, {num1: 21, operator: "*", num2: 6 }],答案为21。

现在,如何解决这个问题,您的方法的问题是,您已经正确地分开了方程,但您不知道哪一部分方程将首先得到解决/计算。因此,在使用这2个for-循环之前,您将无法获得正确的结果。

要解决这个问题,您必须使用TreeStack数据结构。您可以参考https://www.geeksforgeeks.org/expression-tree/获得相同的信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73040531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档