By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,364 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,364 IT Pros & Developers. It's quick & easy.

I like my code to run on click event.

P: 1
I have coded a script and I got confused where I should call the function. I know to call it from the outside of it, but I want to implement a process entry function before the calculate function. I am stuck. I am posting a piece of the coding I have don:
Expand|Select|Wrap|Line Numbers
  1. "use strict";
  2. var $ = function (id) {
  3.     return document.getElementById(id);
  4.     //Asking for user entry.
  5. };
  6.  var processEntry = function() {
  7.             var entry = ($("income").value);
  8.             entry = parseInt($("income").value);
  9.             //Data validation for user entry.
  10.             if (isNaN(entry)) {
  11.                 alert(" Entry must be numeric");
  12.             }
  13.             //Variable declaration for calculate_tax function.
  14.             var tax = calculate_tax(income);
  15.             alert(tax);
  16.              };
  17.           //Creating a calculate function for  taxable income.
  19.      var calculate_tax = function(income,tax){
  20.     //var income = parseFloat( $("income").value );
  21.    // $("tax").value =  calculate_tax(income);
  22.     //alert(tax);
  23.          var tax;     
  24.         // var calculate = 0;
  25.     if(entry <= 9275 ){
  26.         tax = (entry - 9275) * .10;
  27.     }
  28.     else if( entry <= 37650){
  29.     tax = (37650 - entry) * .15 + 927.5;     
  30.     }
I am not finish, but I want the data validation first and then the process entry function may process the calculate function data.
Oct 26 '19 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 5K+
P: 5,389
well - so you have a process_entry function and a calculate_tax function. process_entry does some validation - so call that one onclick and when the validation was successful then call the calculate_tax method. At the moment you call the calculation even when the validation fails so that should be adapted. In line 14 you call the function with an undefined value though - but you you probably want to pass the entry value to it and calculate with it. Variables that you declare in one function are locally scoped - thus are not accessible outside the function - but you can pass them as parameters to other functions of course.

What is the exact problem that you have with it? The title and the description in the post might be a bit confusing at least for me. Is it how to access/passing the values around?

PS: for the parseInt method you should always provide the radix since it can lead to unintended results if its not provided.
Oct 28 '19 #2

Post your reply

Sign in to post your reply or Sign up for a free account.