Adding let values together in JavaScript - javascript

I'm working on a tax calculator app with JavaScript and have a problem adding let values.
When trying to add 3 different tax values together, in the console, the answer I always get is the first of the values.
let basicRate,
higherRate,
additionalRate;
function calculateTaxDue(grossSalary) {
if (grossSalary > 150000) {
basicRate = parseFloat((46351 - 11000) * 0.2).toFixed(2);
higherRate = parseFloat((150000 - 46351) * 0.4).toFixed(2);
additionalRate = parseFloat((grossSalary - 150000) *
0.45).toFixed(2)
taxDue = parseFloat((basicRate + higherRate +
additionalRate)).toFixed(2);
}
}
calculateTaxDue(150001)
console.log(parseFloat(basicRate).toFixed(2));
console.log(parseFloat(higherRate).toFixed(2));
console.log(parseFloat(additionalRate).toFixed(2));
console.log(parseFloat(basicRate + higherRate +
additionalRate).toFixed(2));
Just prints the first value (basicRate) to the console. I'm confused by this.
Apologies for lack of detail first time around.
Thanks

You are getting this result because basicRate, higherRate and additionalRate are strings.
basicRate + higherRate +
additionalRate produces the string "7070.2041459.600.45" and parseFloat("7070.2041459.600.45").toFixed(2) the returns 7070.20.
Only use .toFixed when you actually want to display numbers:
let basicRate,
higherRate,
additionalRate;
function calculateTaxDue(grossSalary) {
if (grossSalary > 150000) {
basicRate = (46351 - 11000) * 0.2;
higherRate = (150000 - 46351) * 0.4;
additionalRate = (grossSalary - 150000) *
0.45;
taxDue = basicRate + higherRate +
additionalRate;
}
}
calculateTaxDue(150001)
console.log(basicRate.toFixed(2));
console.log(higherRate.toFixed(2));
console.log(additionalRate.toFixed(2));
console.log((basicRate + higherRate +
additionalRate).toFixed(2));
Most importantly: Read the documentation of the functions you are using.
parseFloat expects a string as input and returns a number. There is no point in passing a number to it like you do in parseFloat((46351 - 11000) * 0.2).
.toFixed returns a string. Don't use it if you want to actually perform further computations with the number values.
And finally, don't use floating point values to perform monetary computations. The results will be incorrect due to rounding errors and loss of precision. Express all numbers as integers.

Related

What is the purpose of +- on a Javascript number?

I came across some code recently that was like this:
const element = document.getElementById("myId")
const rect = element.getBoundingClientRect()
const height = +-(rect.height / 1)
First of all, what is the deal with the division by 1? And second, what does +- do?
I put that logic into a Fiddle and it appears that it flips the sign of whatever is in the parentheses (from positive to negative and from negative to positive). However, if I wanted to flip a sign, why wouldn't I just do -(myvariable)?
Regarding the division by 1, it appears that the type of rect.height is already a number with floating-point precision and the divide operator is also floating-point division so we're not trying to generate an int or anything.
I just need some help trying to understand what that's trying to do.
Edit: The code was found here: Check if element is partially in viewport
Using division / will implicitly convert both operands to numbers:
const str = "10.5"
const division = str / 1;
console.log(division);
console.log(typeof division);
Using a unary minus - will implicitly convert the operand and change its sign:
const str = "10.5";
const minusStr = -str;
console.log(minusStr);
console.log(typeof minusStr);
const negativeNum = -3;
const minusNegativeNum = -negativeNum;
console.log(minusNegativeNum);
Using a unary plus + will convert anything to a number. If supplied with a number, it leaves it as it is:
const str = "10.5";
const plusStr = +str;
console.log(plusStr);
console.log(typeof plusStr);
const negativeNum = -3;
const plusNegativeNum = +negativeNum;
console.log(plusNegativeNum);
The above is also the order of how the expression +-(rect.height / 1) would be evaluated.
So, what does +-(rect.height / 1) do? The same as -rect.height but tacks on two useless operators.
It should be noted, that none of the conversions are really needed - not because a unary minus already does it, but because the height property produces a number anyway:
const element = document.getElementById("myId")
const rect = element.getBoundingClientRect()
console.log(rect.height);
console.log(typeof rect.height);
const height = +-(rect.height / 1);
console.log(height);
#myId {
width: 400px;
height: 200px;
background: red;
}
<div id="myId"></div>
So the whole expression just gets the height and inverts its sign.
Can you provide the link where you found this code?
But from what you provided, I would agree with you. The + operator and the dividing by one wouldn't do anything. So I would say that it's a typo, bit of temporary code, or the developer having one too many drinks.
I think it´s a trap. I´m not shure but if you get numbers from document you get strings instead of numbers. this + before a number in a string (example "10") would turn it in type number.
For example
"11" + 1 = "111"
because javascript concanate this as 2 strings.
but
var a = "11"
+a makes it = 11
but the restit sadly out of context i think
Edit:
ah okay.
Math.floor(100 - (((rect.top >= 0 ? 0 : rect.top) / +-(rect.height / 1)) * 100)) < percentVisible
+-(rect.height / 1)) * 100
I think this parts makes this number to a percent value.JS don´t know percent. Everything is value / 100, but to get the right value, you should value / 1.

JavaScript Math Expression Yields Different Results

The following is a line I have in my Javascript code. It outputs -5108024 and some change when sqftVal = 2828 and bathsVal = 3.5.
out.value = -6932000 + 221400 * Math.log(sqftVal) + 637.2*Math.exp(bathsVal) + 51640;
However, when I manually type this in my calculator, I get roughly -5099721 and some change. I get the same result in R. Why does JavaScript mess up the math, and what can I do to fix this?
Calculator/R input:
-6932000 + 221400 * ln(2828) + 637.2 * e^(3.5) + 51640 = -5099721.073
I don't believe this is a floating point error because as I add more terms, the difference becomes fairly large.
Plus, everything was matching up until I added the fourth term (+51640) which made no sense to me.
There must be some other code that is interfering with your values or something, because the code shown does not produce the value you report.
var sqftVal = 2828;
var bathsVal = 3.5;
var value = -6932000 + 221400 * Math.log(sqftVal) + 637.2*Math.exp(bathsVal) + 51640;
console.log(value);

Why would we declare a 2nd variable in the below code?

Why would we declare a second variable (val) when we can use the parameter of the function as a variable?
Here's how it looks like on codecademy:
var divideByThree = function (number) {
var val = number / 3;
console.log(val);
};
divideByThree(6);
I've made some changes as below:
var divideByThree = function (number) {
number = number / 3;
console.log(number);
};
divideByThree(6);
And it works pretty fine!!
In your example, you do not need to preserve the original value of the parameter. However, you may find it easier to use extra variables in the future for more complicated functions.
Here is an example:
// This function uses the parameter "rawNumber" as a variable, but also uses an extra variable "number"
function TestThis(rawNumber, p) {
// Convert the input (string) to integer
// parseInt returns NaN or integer. Truncates decimals
var number = parseInt(rawNumber);
// Check to see if the result is NaN or is an integer
if (isNaN(number)) {
Log(rawNumber + " is not a number.", p); // Log is my imitation of console.log()
}
// will run if number is type int
else {
if (number > 0 && number <= 100) {
Log(rawNumber + " is a valid number.", p);
} else {
Log(rawNumber + " is not between 1 and 100.", p);
}
}
}
You can see this code working in this Fiddle.
In this function I used an extra variable called "number" in three different places. I didn't have to, but it was easier than typing isNaN(parseInt(rawNumber)) and if(parseInt(rawNumber) > 0 && parseInt(rawNumber) <= 100). Codecademy was probably decided to teach you this way because it is easier to realize you can simplify your code than to realize you can simplify a more complex code through the use of extra variables.
Also, JK Price's answer brings up a readability issue. Simply put, this code is easier to read and understand:
function Example(number) {
var processedNumber = 5/(Math.log(1/number*3.14 - 7));
console.log("Message: " + (processedNumber * 2));
console.log("Message: " + (processedNumber / 10));
}
This code might be a little harder:
function Example(number) {
console.log("Message: " + ((5/(Math.log(1/number*3.14 - 7)) * 2));
console.log("Message: " + ((5/(Math.log(1/number*3.14 - 7)) / 10));
}
Variables are supposed to help the programmer write better and describe a better story. You can't have the same actor play multiple characters! One thing it does is to help keep variables separate.
The variable val in this case helps abstract the logic and most importantly help in debugging. If this was a long script and you saw that number was not what you originally passed it, you might consider it to be an error.

toFixed Isn't Doing Anything

I'm teaching myself JavaScript and have run into a problem with toFixed(). I'm working through an amortization calculator; and, one of the steps returns a number with a huge number of decimal places. I'm trying to cut it down to 4 decimal places.
Be advised the sample code has a lot of explanatory HTML in it. It's only there so that I can work through the steps of the equation. Also, when I add one to the very long number, it adds the numeral one to end of the scientific notation.
var paymentamount;
var principal=250000;
var interestrate = 4.5;
var annualrate = interestrate/12;
var numberofpayments = 360;
document.write("This is the annuitized interest rate: "+ annualrate +"%");
document.write("<h3> Now we add 1 to the annualized interest rate</h3>");
var RplusOne = annualrate + 1;
document.write("<p> This is One Added to R: " + RplusOne + "%");
document.write("<h3>Next RplusOne is Raised to the power of N </h3>");
var RRaised = (Math.pow(RplusOne, numberofpayments)).toFixed(4);
document.write("<p>This gives us the following very long number, even thought it shouldn't: " + RRaised);
document.write("<h3>Now we add one to the very long number </h3>");
var RplusOne = RRaised + 1;
document.write("<p>Now we've added one: " + RplusOne);
From MDN's documentation:
If number is greater than 1e+21, this method simply calls Number.prototype.toString() and returns a string in exponential notation.
The problem is that you are using 4.5 as your interest rate instead of 0.045, so doing this:
Math.pow(4.5 / 12 + 1, 360)
gives you a huge number (6.151362770461608e+49 or 6.15 * 10^49 to be exact). Change your interest rate to 0.045 and you will get what you are expecting.
As for the var RplusOne = RRaised + 1 line, the problem here is that RRaised is a string because of toFixed. I would only call toFixed when you're displaying things, and not at any other time; the primary reason for this would be to avoid rounding errors in subsequent calculations, but has the added benefit that your variables remain numbers and not strings.

Returning function value from array using random selection

Been running into some problems with returning the computed value of a function that is inside an array. and would appreciate any help in solutions as well as advice about more elegant ways of approaching this type of problem (i.e. a more logical way to have solved this)
I'm trying to create a program that generates two random digits and a random operator to apply to said digits.
var num1 = Math.floor(Math.random() * 20 + 1);
console.log(num1);
var num2 = Math.floor(Math.random() * 1000 + 21);
console.log(num2);
I originally set my random operator this way:
var ops = ["+", "-", "*", "/"]
I tried to use a Math.random function to select a random index number for the ops array, but was getting all kinds of errors, so I started searching and found some helpful advice here: How to use Javascript to ask the user random math questions?, but couldn't figure out a way to retrieve a random value from the object,(cf. Access non-numeric Object properties by index?) so I changed back to using an array of 4 functions, each of which has a return value that computes the random numbers (num1 and num2) based on the random operator.
I've gotten everything to work (i.e. have tested my random number functions are working, and even see that my newOp variable is returning a different function from the ops array each time), but here's what I can't get the program to do: return the computed value of the function. When I
alert(newOp)
I want it to alert the function's value instead of the function itself. (e.g. alert -> 20 instead of what I'm currently getting: --> function a(){return (num1 + num2)}
Here's my code. All advice and insight welcome! Just trying to learn.
var num1 = Math.floor(Math.random() * 20 + 1);
console.log(num1);
var num2 = Math.floor(Math.random() * 1000 + 21);
console.log(num2);
var ops = [
function a(){return (num1 + num2)},
function b(){return (num1 - num2)},
function c(){return (num1 * num2)},
function d(){return (num1 / num2)}];
var problem = function () {
var random = Math.floor(Math.random()* 4 + 0);
var newOp = ops[random];
alert(newOp);
};
problem();
I think you just need to change it to:
alert(newOp());
Your array contains references to the functions, but you're not invoking them.
I think you are just missing the actual invocation.
Change alert(newOp); to alert(newOp());

Categories

Resources