I'm stumped trying to identify the issue in the JavaScript code provided. I attempted to save a function within a variable with no success

After incorporating the suggestions I received, I have revised the entire code. However, there seems to be a missing element as I am still unable to utilize the var deducere in mathematical operations. I consistently receive NaN. Any advice or feedback on my code would be greatly appreciated, as I have reached a point where I am out of ideas. It appears that this process is indeed the most effective way to learn.

function calculateIncome() {
        var gross_income = document.getElementById("venit_brut").value;
        var selection = document.getElementById("select1").value;
        var social_security = (10.5 / 100) * gross_income;
        var health_insurance = (5.5 / 100) * gross_income;
        var unemployment = (0.5 / 100) * gross_income;
        var num_tickets = document.getElementById("nr_tichete").value;
        var ticket_value = document.getElementById("val_tichete").value;
        var tickets = num_tickets * ticket_value;
        var tax = (16 / 100) * (income + tickets);
        if (gross_income != null) {
            document.getElementById("venit_brutx").innerHTML = Math.round(gross_income);
            document.getElementById("cas").innerHTML = Math.round(social_security);
            document.getElementById("cass").innerHTML = Math.round(health_insurance);
            document.getElementById("somaj").innerHTML = Math.round(unemployment);
            document.getElementById("venitx").innerHTML = Math.round(income);
            document.getElementById("tichete").innerHTML = Math.round(tickets);
            document.getElementById("impozit").innerHTML = Math.round(tax);
            document.getElementById("venit_net").innerHTML = Math.round(net_income);
        }
        };


        function calculateDeduction(selection, gross_income) {
        var deduction0 = (1 - (gross_income - 1500) / 1500) * 300;
        var deduction1 = (1 - (gross_income - 1500) / 1500) * 400;
        var deduction2 = (1 - (gross_income - 1500) / 1500) * 500;
        var deduction3 = (1 - (gross_income - 1500) / 1500) * 600;
        var deduction4 = (1 - (gross_income - 1500) / 1500) * 800;
        if (selection == 0 && gross_income <= 1500) {
            return 300;
        }
        if (selection == 0 && gross_income > 1500 && gross_income <= 3000) {
            return Math.ceil10(deduction0, 1);
        }
        if (selection == 1 && gross_income <= 1500) {
            return 400;
        }
        if (selection == 1 && gross_income > 1500 && gross_income <= 3000) {
            return Math.ceil10(deduction1, 1);
        }
        if (selection == 2 && gross_income <= 1500) {
            return 500;
        }
        if (selection == 2 && gross_income > 1500 && gross_income <= 3000) {
            return Math.ceil10(deduction2, 1);
        }
        if (selection == 3 && gross_income <= 1500) {
            return 600;
        }
        if (selection == 3 && gross_income > 1500 && gross_income <= 3000) {
            return Math.ceil10(deduction3, 1);
        }
        if (selection == 4 && gross_income <= 1500) {
            return 800;
        }
        if (selection == 4 && gross_income > 1500 && gross_income <= 3000) {
            return Math.ceil10(deduction4, 1);
        }
        if (document.getElementById("btn_deducere").checked == false) {
            document.getElementById("deducerex").innerHTML = 0;
        }
        if (gross_income > 3000)  {
        return 0;
        }
        };

    var deduction = calculateDeduction(selection, gross_income);
    var income = gross_income - (social_security + health_insurance + unemployment + deduction);
    var net_income = income - tax + deduction;

        document.getElementById("deducerex").innerHTML = Math.round(deduction);

Answer №1

var deduction0 = ...,
    deduction1 = ...,
    deduction2 = ...,    
    deduction3 = ...,
    deduction4 = ...;


function calculateBenefit(selection, grossIncome) {
    if (selection == 0 && grossIncome <= 1500) {
        return 300;
    }
    if (selection == 0 && grossIncome > 1500 && grossIncome <= 3000) {
        return Math.ceil10(deduction0, 1);
    }
    if (selection == 1 && grossIncome <= 1500) {
        return 400;
    }
    if (selection == 1 && grossIncome > 1500 && grossIncome <= 3000) {
        return Math.ceil10(deduction1, 1);
    }
    if (selection == 2 && grossIncome <= 1500) {
        return 500;
    }
    if (selection == 2 && grossIncome > 1500 && grossIncome <= 3000) {
        return Math.ceil10(deduction2, 1);
    }
    if (selection == 3 && grossIncome <= 1500) {
        return 600;
    }
    if (selection == 3 && grossIncome > 1500 && grossIncome <= 3000) {
        return Math.ceil10(deduction3, 1);
    }
    if (selection == 4 && grossIncome <= 1500) {
        return 800;
    }
    if (selection == 4 && grossIncome > 1500 && grossIncome <= 3000) {
        return Math.ceil10(deduction4, 1);
    }
    if (document.getElementById("btn_deduction").checked == false) {
        return 0;
    }

    return 0;
};

var benefitAmount = calculateBenefit(selection, grossIncome);
var income = grossIncome - (cas + cass + unemployment + benefitAmount);

console.log(income);
console.log(benefitAmount);
  • Ensure that deduction01234 are properly declared
  • Include additional console.log statements to verify the function's output
  • Rename benefitAmount to a more descriptive name to clarify its purpose

Hope this explanation proves helpful :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Steps for passing a Javascript variable through POST method and then redirecting to a different page

I am facing an issue with sending a JavaScript variable via POST to a PHP page and displaying the new page. Despite checking the variable using Wireshark and confirming that it is sent correctly (key and value), the new page does not show anything. $(docu ...

After employing a combination of forEach and filter queries in JavaScript to sift through dropdown options, no results are being generated

Hey there fellow developers, I'm currently in the process of developing an app and have reached the stage where I am filtering elements using dropdowns. I am querying a JSON object and comparing it with the selected element in the dropdown at that mom ...

Utilizing Express Session with Vue CLI in a Node.js Environment

Developing a nodejs backend for my Vue application has brought up a challenge regarding user sessions and database operations. I initially tried using express-session, but the sessions appeared as undefined in subsequent requests. How can I address this is ...

What is the best way to increase incremental values that are nested within each other

A database has been loosely created with a key known as website. Inside this website object, multiple objects exist, one for each dynamically generated website. An illustration of how the database might appear is shown below: website: { google.com: { ...

How does React-router handle component reloading when there is a change in the state of the parent component

Within my React application, I've integrated a ResponsiveDrawer component sourced from material-ui's demo. This component essentially displays a drawer containing a list of menu items, a title bar, and a content frame. The state of the component ...

Updating choices within a div in real-time by changing the select box dynamically

I need to swap out the select box that is nested inside this div <div class="models"> <select disabled="disable"> <option>Model Name</option> </select> </div> I am attempting to target the div and manipul ...

While attempting to deploy my project on Vercel by pulling in my code from GitHub, I ran into this error

As a self-taught Front End developer diving into building and hosting my first web page using React, I've encountered this warning. Any suggestions on how to resolve it? Cloning gitohub.com/Passion94/React-Apps (Branch: main, Commit: da89a2a) Cloning ...

Alter the onSeries property dynamically

If there are 3 different series identified by the IDs series1, series2, and flags, where initially the onSeries property of flags is set to series1. In a scenario where I click on the legend to hide series1, is it possible within the legendItemClick even ...

Position the div in the center of a container that is 100% width and has floating elements with dynamic widths

I am looking to align 3 divs inside a container div, with the arrangement of [LEFT] [CENTER] [RIGHT]. The container div should be 100% wide without a fixed width, and I want the center div to stay centered even when resizing the container. The left and ...

Experiencing difficulties loading Expo Vector Icons in Nextjs

I've spent countless hours trying various methods to accomplish this task, but unfortunately, I have had no luck so far. My goal is to utilize the Solito Nativebase Universal Typescript repository for this purpose: https://github.com/GeekyAnts/nativ ...

ngFor returning undefined despite array containing value

While iterating through an array using ngFor, I'm encountering an issue where trying to access data in the 'value' variable results in it being undefined. Why is this happening? myArray = ['a', 'b', 'c', 'd ...

Display a D3 Collapsible Tree visualization using information stored in a variable

I am currently working on an app that requires the display of a collapsible tree graph using D3. The data needed for this graph is not stored in a file, but rather within the database. It is retrieved through an Ajax call to a rest service and then passed ...

Best practice for integrating Typescript into an established ASP.NET 4 Webforms project

Currently, I am working on an older asp.net 4.0 Webforms project using Visual Studio 2015. My goal is to transition from using Javascript to TypeScript for certain client side code tasks. While I have experience using TypeScript in projects outside of Vis ...

When dynamically adding an option, the select value becomes empty

I am facing a problem with a select element that I cannot change programmatically. Initially, the select has only one option: The initial HTML structure looks like this: <select id="clients" name="client"> <option>Existing Clients...</ ...

Node.js: Verifying the user's previous login status using Passport

My current express router for users handles user logins using a token system: var express = require('express'); var router = express.Router(); var passport = require('passport'); var User = require('../models/user'); var Veri ...

Using jQuery to decrease the size of a div element containing an image

Hello everyone! I have an image inside a div and I am currently moving it down at a specific time using a delay. My question is, how can I make the image shrink as it moves down the screen until it eventually disappears completely at a set location? Curre ...

Guide to obtaining specific top elements from an array using JavaScript

I'm seeking assistance with sorting arrays in JavaScript. Here is an example of a sorted array: mainArray : [25 20 20 20 18 17 17 15 12 12 10 5 5 ] The mainArray may contain duplicate values. A. Dealing with duplicates Based on user input, I need ...

When using Javascript innerhtml, it fails to recognize and properly parse Twig tags

I have a function in Twig that retrieves values from a database and displays them in a select box. I am attempting to update the content of the div, but I am facing an issue with innerHTML. When using {{ without quotes, it creates a new line which is flagg ...

Next.js triggers the onClick event before routing to the href link

Scenario In my current setup with Next.js 13, I am utilizing Apollo Client to manage some client side variables. Objective I aim to trigger the onClick function before navigating to the href location. The Code I'm Using <Link href={`/sess ...

Extracting text from a JSON file and populating text fields: A step-by-step guide

Utilizing this JSFIDDLE to duplicate text fields has been quite helpful. I am now looking to create JSON data based on the input from all the text fields. When it comes to updating a page, I need to extract the JSON data and accurately populate the corresp ...