简体   繁体   English

尝试锻炼如何处理数组

[英]Trying to workout how to deal with an array

I'm in the process of teaching myself javascript and it looks at though I've hit a road block. 我正在自学JavaScript,尽管我遇到了障碍,但它仍在研究中。 As an exercise I am trying to build a tax calculator. 作为练习,我试图构建一个税收计算器。

Here is an object I built that contains the tax brackets: 这是我构建的包含税项的对象:

var taxBracket = [
{bracket: 1, from:0, to:18200, percentage :0, amount:0},
{bracket: 2, from:18201, to:37000, percentage :19, over:18200, amount:0},
{bracket: 3, from:37001, to:80000, percentage :32.5, over:37000, amount:3752},
{bracket: 4, from:80001, to:180000, percentage :37, over:80000, amount:17547},
{bracket: 5, from:180001, to:0, percentage :45, over:180000, amount:54547}];

and here is the function that loops through object to find the corresponding tax bracket of y 这是遍历对象以查找y的相应税级的函数

function returnTax (y){
    for(var x in taxBracket){
    if (y >= taxBracket[x].from && y <= taxBracket[x].to){
        var z = taxBracket[x].amount + ((grossIncome-taxBracket[x].over) * (taxBracket[x].percentage/100));
        return z;   
    }
};

The issue is that if y is over 180000, it errors out as to is 0. Is this only solution to this an else statement repeating the functions of the if statement? 问题是,如果y超过18万,它的错误了作为to 0这是唯一的解决方案,以这样的一个else语句重复if语句的功能是什么? Thank for your help! 谢谢您帮忙!

Good job with it so far. 到目前为止,做得很好。 Simply using Infinity instead of defaulting to 0 on the final bracket will fix it. 只需使用Infinity而不是最后一个括号中的默认值即可解决此问题。 I've made a couple of recommendations on your syntax as well, hope it helps. 我也对您的语法提出了一些建议,希望对您有所帮助。

var taxBracket = [
    {bracket: 1, from: 0, to: 18200, percentage: 0, amount: 0},
    {bracket: 2, from: 18201, to: 37000, percentage: 19, over: 18200, amount: 0},
    {bracket: 3, from: 37001, to: 80000, percentage: 32.5, over: 37000, amount: 3752},
    {bracket: 4, from: 80001, to: 180000, percentage: 37, over: 80000, amount: 17547},

    // Use Infinity instead of 0 for the "to" value of bracket 5
    {bracket: 5, from: 180001, to: Infinity, percentage: 45, over: 180000, amount: 54547}
];

// Variable names like 'x', 'y' and 'z' can cause problems with code readability.
// Descriptive names such as "income", or even "string" or "int" will help you out when you come to review your code later!
function returnTax(y){

    // Using for..in with arrays can cause issues with enumerating over object properties.
    // You don't have to worry about it in this case, but a standard for loop (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) is better practice.
    for(var x = 0; x < taxBracket.length; x++){

        if(y <= taxBracket[x].to){

            // I find breaking up long calculations into separate variables can make it
            // more readable. More preference than anything else though!
            var amountOver = grossIncome - taxBracket[x].over;
            var percent = taxBracket[x].percentage / 100;
            return taxBracket[x].amount + (amountOver * percent);

        }

    }

}

You are creating an array literal of object literals. 您正在创建对象文字的数组文字。 You should loop through arrays with a standard for loop. 您应该使用标准的for循环遍历数组。

一种解决方案是使最后一个“ to”字段的数量非常大。

you can use built in function to loop array of object like 您可以使用内置函数来循环对象数组

yourArray.forEach
( function (arrayItem)
{
var x =
arrayItem.prop1 + 2;
alert(x);
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试锻炼如何使用四元数旋转沿路径移动的相机以寻找新的方向向量 - Trying to workout how to use quaternions to rotate a camera that is moving along a path to look in new direction vector 如何处理查询中的数组值? - How to deal with array value in query? 如何将参数作为值或值数组处理 - How to deal with parameter as a value or an array of values 如何处理大数组(100.000个对象)? - How to deal with big array (100.000 objects)? 如何使用javascript获取和处理Chrome URL(尝试开发Chrome扩展程序) - How to get and deal with Chrome URL with javascript (Trying to develop a Chrome Extension) 如何在一个iMacros脚本中使用多个循环进行锻炼? - How to workout with more than one loop in one iMacros script? 我们如何处理包含带有秘银子阵列的数据模型? - How can we deal with a data model containing child array with mithril? 尝试对“锻炼”对象发出 POST 请求时出现错误 - I am getting an error while trying to make a POST request of the "workout" object 在尝试模拟 localStorage 时,如何处理来自 Test in react 的 JSON.parse 错误? - How do I deal with a JSON.parse error coming from a Test in react when trying to mock localStorage? 尝试在C#.NET / WPF中获取网页时如何处理JavaScript? - How to deal with JavaScript when trying to fetch web page in C#.NET/WPF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM