Limit to 2 decimals result in JavaScript - javascript

I have an issue with the decimals in a result in my script, it gives unlimited decimals, I want to limit it to 2, I have tried using to.fixed(2) without luck, but the script broke.
This is my script:
$.each(rows, function () {
quantity = $(this).find('[data-quantity]').val();
if (quantity == '') {
quantity = 1;
$(this).find('[data-quantity]').val(1);
}
_amount = parseFloat($(this).find('td.rate input').val()) * quantity;
$(this).find('td.amount').html(_amount);
subtotal += _amount;
row = $(this);
item_taxes = $(this).find('select.tax').selectpicker('val');
if (item_taxes) {
$.each(item_taxes, function (i, taxname) {
taxrate = row.find('select.tax [value="' + taxname + '"]').data('taxrate');
calculated_tax = (_amount / 100 * taxrate);
if (!taxes.hasOwnProperty(taxname)) {
if (taxrate != 0) {
_tax_name = taxname.split('|');
tax_row = '<tr class="tax-area"><td>' + _tax_name[0] + '(' + taxrate + '%)</td><td id="tax_id_' + slugify(taxname) + '"></td></tr>';
$(discount_area).after(tax_row);
taxes[taxname] = calculated_tax;
}
} else {
// Increment total from this tax
taxes[taxname] = taxes[taxname] += calculated_tax;
}
});
}
});
The code line that does the operation is:
calculated_tax = (_amount / 100 * taxrate);

The correct is toFixed(), not to.fixed(), see the example:
let number = 12.128361;
let _amount = number.toFixed(2);
let calculated_tax = (_amount / 100 * 100);
console.log(calculated_tax);

Use this method to round to any amount of decimal places:
let v = 3.486894716724;
let rounded = Math.round(v * 100) / 100; // => 3.49
Multiply and divide by 10 to round to one DP, 100 for 2DP, 1000 for 3, etc...

Related

Grading Calculator Equation for lesser grades

I'm trying to create a calculator that when you input your points, and the total amount of points in the class you receive a lot of information about it, I currently have working the letter grade, percentage, and the amount of points that you need to get to the next grade up.
Ex. 95/100
A 95%
Points Needed = 5
I want to do the same thing with Points needed but opposite. So with the same example it would be
Points Lost = 5
I currently have the script for my current Points Needed if it helps, but I really can't figure out the equation for this, luckily not necessary for a school project, just for a solo project so I can learn how to code.
Here is the code for the Points Needed
var pointsNeeded = totalGrade.value - grade.value;
if (gradePercentage > 100) {
pointsNeeded = pointsNeeded * -1;
document.getElementById("pointsNeeded").innerHTML = "You can lose " + pointsNeeded + " point(s) and still have 100%";
}
else {
document.getElementById("pointsNeeded").innerHTML = pointsNeeded;
}
}
else if (gradePercentage < 89, gradePercentage > 79) {
var pointsNeeded = Math.round(10 * ((totalGrade.value * 0.9) -
grade.value)) / 10;
document.getElementById("pointsNeeded").innerHTML = pointsNeeded;
}
else if (gradePercentage < 79, gradePercentage > 69) {
var pointsNeeded = Math.round(10 * ((totalGrade.value * 0.8) -
grade.value)) / 10;
document.getElementById("pointsNeeded").innerHTML = pointsNeeded;
}
else if (gradePercentage < 69, gradePercentage > 59) {
var pointsNeeded = Math.round(10 * ((totalGrade.value * 0.7) -
grade.value)) / 10;
document.getElementById("pointsNeeded").innerHTML = pointsNeeded;
}
else if (gradePercentage < 59) {
var pointsNeeded = Math.round(10 * ((totalGrade.value * 0.6) -
grade.value)) / 10;
document.getElementById("pointsNeeded").innerHTML = pointsNeeded;
}
gradePercentage being the actual percentage, totalGrade being the full grade, and grade being the grade you have.
Also these are the grade values,
var gradeLetter = "A"
document.getElementById("result").innerHTML = gradeLetter + " " + result + "%";
}
if (gradePercentage > 79.9, gradePercentage < 90) {
var gradeLetter = "B"
document.getElementById("result").innerHTML = gradeLetter + " " + result + "%";
}
if (gradePercentage > 69.9, gradePercentage < 80) {
var gradeLetter = "C"
document.getElementById("result").innerHTML = gradeLetter + " " + result + "%";
}
if (gradePercentage > 59.9, gradePercentage < 70) {
var gradeLetter = "D"
document.getElementById("result").innerHTML = gradeLetter + " " + result + "%";
}
if (gradePercentage < 59.9) {
var gradeLetter = "F"
document.getElementById("result").innerHTML = gradeLetter + " " + result + "%";
}
This code is pretty self-explanatory:
let startingPoints = 952.3 // How many points you got on the test
let maxPoints = 1049 // Max possible points you could have gotten
// Your grade (0-100%):
let gradePercentage = 100 * startingPoints / maxPoints; // E.g. 90.7816968541468
// How much less you'd need (as a percentage) to get the next lowest grade:
let extraPercentage = ( gradePercentage % 10 ) + 0.01; // E.g. 0.7916968541468015
// Your new grade (as a percentage) if you bumped it down:
let newGrade = gradePercentage - extraPercentage; // E.g. 89.99
// How many points you'd have to reduce your score by to bump it down to the next lowest
let extraPoints = ( extraPercentage / 100 * maxPoints ) // E.g. 8.304899999999947
// What your score would be if you bumped it down to the next level:
let newExtraPoints = startingPoints - extraPoints; // E.g. 943.9951
console.log( `Your grade is ${gradePercentage}% (${startingPoints} points out of a possible ${maxPoints}). If you subtract ${extraPercentage}% (or ${extraPoints} points), your grade will be ${newGrade}% (${newExtraPoints} points)` );
The only trickiness is that we're adding 0.01, which technically could be 0.001, or 0.0001, or...well, we have to add something to drop below the limit, and with no set "right" amount, I'm choosing 0.01 because it works out nicely.
Example output:
Your grade is 90.7816968541468% (952.3 points out of a possible 1049).
If you subtract 0.7916968541468015% (or 8.304899999999947 points),
your grade will be 89.99% (943.9951 points)

Why is my total multiplier adding a pattern of 2 to each answer?

function getTotal() {
let total = 0;
for (var i = 0; i < 10; i++) {
let valueToAdd = i;
var multiplier = 2;
total += valueToAdd * multiplier;
console.log(total);
}
return total;
}
console.log(
getTotal()
);
It was my estimation, that at each iteration of the loop, the total should be:
a) 2, 4, 6, 8, 10... etc
but instead, I am getting the following answers: b) 0,2,6,12,20,30,42..... 90
Where in my code is the pattern of this ensuing addition being replicated?
Why am I not getting the original answer I intended in a)?
Just use total = valueToAdd * multiplier; instead of adding the result of each iteration to total to get that output you expect.
function getTotal() {
let total = 0;
for (var i = 0; i < 10; i++) {
let valueToAdd = i;
var multiplier = 2;
total = valueToAdd * multiplier;
console.log(total);
}
return total;
}
console.log(getTotal());
Also note that it returns 0, to avoid that you can start your iteration with i=1 and condition as i<=10.
You are adding and multiplying both by using this
total += valueToAdd * multiplier;
So, If you want to implement by multiplying, you should be doing
total = valueToAdd * multiplier;
but if you want to implement by adding, you should be doing
total += multiplier;
On third iteration
total += valueToAdd * multiplier
Translates to
2 += 2 * 2
With your intended result your code should be
total = valueToAdd * multiplier
So each iteration value of total will be overriden by the formula
While the current one appends the result of formula to variable total, in this case (integer data type) will be summed
Due to my low reputation ( < 50 ), I can't add comment. So I make it a new answer.
total += valueToAdd * multiplier; is the same as 「total = total + valueToAdd * multiplier;」
because total, valueToAdd and multiplier are all numbers, the + operator will do arithmetic addition.So
total = total + valueToAdd * multiplier = 0 + 0 * 2 = 0 ( when i = 0 )
total = total + valueToAdd * multiplier = 0 + 1 * 2 = 2 ( when i = 1 )
total = total + valueToAdd * multiplier = 2 + 2 * 2 = 6 ( when i = 2 )
total = total + valueToAdd * multiplier = 6 + 3 * 2 = 12 ( when i = 3 )
total = total + valueToAdd * multiplier = 12 + 4 * 2 = 20 ( when i = 4 )
that's why you get 0,2,6,12,20...
But if we change the line 「let total = 0;」 to 「let total = '0';」. Now the + operator will be used to concatenate (add) strings
total = total + valueToAdd * multiplier = '0' + 0 * 2 = '0' + 0 = '00' ( when i = 0 )
total = total + valueToAdd * multiplier = '00' + 1 * 2 = '00' + 2 = '002' ( when i = 1 )
total = total + valueToAdd * multiplier = '002' + 2 * 2 = '002' + 4 = '0024' ( when i = 2 )
total = total + valueToAdd * multiplier = '0024' + 3 * 2 = '0024' + 6 = '00246' ( when i = 3 )
total = total + valueToAdd * multiplier = '00246' + 4 * 2 = '002468' ( when i = 4 )
you'll get the final string '002468...18' as the result.
If this is what you want, and you want the string stars from '2'. Do the following modification
let total = ''; // an empty string
for (var i = 1; i < 10; i++) { // i starts from 1
and you will get '24681012141618'.

Generate random number between 000 to 999, and a single digit can only occur maximum of two times

How can I generate numbers that range from 000 to 999? Also, a single digit can only occur maximum of two times in the same number.
Examples of numbers I'd like to generate:
094
359
188
900
004
550
Examples of numbers I don't want to generate:
000
999
444
What I've got so far:
function randomNumbers () {
var one = Math.floor(Math.random() * 9) + 0;
var two = Math.floor(Math.random() * 9) + 0;
var three = Math.floor(Math.random() * 9) + 0;
return '' + one + two + three;
};
I know the code can be improved a lot, I just don't know how. Current function isn't checking if the same number occurs three times (should only occur a maximum of two).
I can use jQuery in the project.
Here is a solution that will never have to retry. It returns the result in constant time and spreads the probability evenly among the allowed numbers:
function randomNumbers () {
var val = Math.floor(Math.random() * 990);
val += Math.floor((val+110)/110);
return ('000' + val).substr(-3);
};
// Test it:
var count = new Array(1000).fill(0);
for (i=0; i<100000; i++) {
count[+randomNumbers()]++;
}
// filter out the counters that remained zero:
count = count.map((v,i) => [i,v]).filter( ([i,v]) => !v ).map( ([i,v]) => i );
console.log('numbers that have not been generated: ', count);
You could count the digits and check before return the value.
function getRandom() {
var count = {};
return [10, 10, 10].map(function (a) {
var v;
do {
v = Math.floor(Math.random() * a);
} while (count[v] && count[v] > 1)
count[v] = (count[v] || 0) + 1;
return v;
}).join('');
}
var i = 1000;
while (i--) {
console.log(getRandom());
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
Try this :
$(document).ready(function() {
for(var i=0; i<50;i++)
console.log(randomNumbers ());
function randomNumbers () {
var one = Math.floor(Math.random() * 9);
var two = Math.floor(Math.random() * 9);
var three = Math.floor(Math.random() * 9);
if( one == two && two == three && one == three )
randomNumbers();
else
return (""+one + two + three);
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
The following should give you the expected result:
function getRandom() {
var randomNo = Math.floor(Math.random() * 999);
var splitted = randomNo.toString().split("");
var res = [];
splitted.filter(v => {
res.push(v);
if (res.filter(x => x == v).length > 2) return false;
return true;
});
while (splitted.length < 3) {
var ran = Math.floor(Math.random() * 9);
if (splitted.indexOf(ran) < 0) splitted.push(ran);
}
console.log(splitted.join(""))
return splitted.join("");
}
for (x = 0; x<100;x++) {getRandom()}
Keeps track of what you started.
function randomNumbers () {
var one = Math.floor(Math.random() * 10);
var two = Math.floor(Math.random() * 10);
var three = Math.floor(Math.random() * 10);
return one==two && two==three ? randomNumbers(): '' + one + two + three;
};
Here the function call itself recursively up to the point where it result
meets the requirements. The probability of generating three equal numbres is 1/100 so be sure that it will recurse nearly to never.
If, it were me, to be more flexible, I'd write a function I could pass parameters to and have it generate the numbers like the below:
function getRandoms(min, max, places, dupes, needed) {
/**
* Gets an array of random numbers with rules applied
* #param min int min Minimum digit allowed
* #param mas int min Maximum digit allowed
* #param dupes int Maximum duplicate digits to allow
* #param int needed The number of values to return
* #return array Array of random numbers
*/
var vals = [];
while (vals.length < needed) {
var randomNum = Math.floor(Math.random() * max) + min;
var digits = randomNum.toString().split('');
while (digits.length < places) {
digits.push(0);
}
var uniqueDigits = digits.removeDupes();
if ((places - uniqueDigits.length) <= dupes) vals.push(digits.join(''));
}
return vals;
}
// for convenience
Array.prototype.removeDupes = function() {
/**
* Removes duplicate from an array and returns the modified array
* #param array this The original array
* #return array The modified array
*/
var unique = [];
$.each(this, function(i, item) {
if ($.inArray(item, unique)===-1) unique.push(item);
});
return unique;
}
var randomNumbers = getRandoms(0, 999, 3, 2, 10);
console.log(randomNumbers);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Generate incremented random numbers between 0 to 100 every second where new number should be greater than the previous nymber

I need a javascript where with every second call i get a random number between 0-100 where current number should be greater than previous number. I wrote a code to get random numbers every second but stuck at generating it with increment.
<script>
var span = document.getElementsByTagName("span")[3];
var i = 100;
(function randNumber() {
var a = Math.floor((Math.random() * i) + 1);
span.innerHTML = "RandNumber: " + a;
setTimeout( randNumber, 1000);
})();
Note : The numbers should generate randomly.
example result may be : 2,5,7,8,22,23,34,56,78,88.....
You should not create a new random number between zero and your maximum (i), but just between the last created number (lastNumber) and max (i).
Also you might want to stop, when the random numbers reached the maximum.
var span = document.getElementsByTagName("span")[3],
i = 100,
lastNumber = 0;
function randNumber() {
lastNumber = lastNumber + Math.floor( Math.random() * (i - lastNumber) + 1 );
span.innerHTML = lastNumber;
if( lastNumber < i ) {
setTimeout( randNumber, 1000 );
}
}
randNumber();
AS for the comments and the requirement of a minimum amount of steps until reaching the maximum:
In each iteration, just increase you number by a random value between 1 and ((max - min) / steps. It is pretty much the same code as above.
var span = document.getElementsByTagName("span")[3],
max = 100,
min = 0,
lastNumber = 0,
minSteps = 30;
// how wide can the average step be at most?
var stepWidth = (max - min) / minSteps;
function randNumber() {
lastNumber = lastNumber + Math.floor( Math.random() * stepWidth + 1 );
span.innerHTML = lastNumber;
if( lastNumber < max ) {
setTimeout( randNumber, 1000 );
}
}
randNumber();
If you extract the "between two numbers" logic into its own function like this, this becomes a lot easier. This way, you just generate a number between your last generated number and your maximum.
var max = 100
var last_number = 1
var interval_id = setInterval(function(){
last_number = get_random_number_greater_between(last_number, max)
span.innerHTML = "RandNumber: " + last_number;
if(last_number >= max){
window.clearInterval(interval_id)
}
}, 1000)
function get_random_number_greater_between(low, high){
return Math.floor(Math.random() * (high - low + 1) + low);
}
Try this:
// generate a random number from 0 to max - 1.
function rand(max) {
return Math.floor(max * Math.random());
}
// generate a random number from min to max.
function range(min, max) {
return min + rand(1 + max - min);
}
// the loop function
function next(n, callback) {
var m = range(n, 100);
if (m < 100) setTimeout(next, 1000, m + 1, callback);
callback(m);
}
var span = document.getElementById("rand");
next(0, function (m) {
span.innerHTML = "Random number: " + m;
});
<span id="rand"></span>

Javascript Greedy Algorithm Making Change

I'm working on the classic "making change" problem, which is highly documented in plenty of other languages, but there's not much out there for it in Javascript. So far, I have this:
var total = $('#total').val();
var coins = [];
function makeChange(total, coins) {
var remainder = 0;
if (total % 0.25 < total) {
coins[3] = parseInt(total / 0.25);
remainder = total % 0.25;
total = remainder;
}
if (total % 0.10 < total) {
coins[2] = parseInt(total / 0.10);
remainder = total % 0.10;
total = remainder;
}
if (total % 0.05 < total) {
coins[1] = parseInt(total / 0.05);
remainder = total % 0.05;
total = remainder;
}
coins[0] = parseInt(total / 0.01);
}
function showChange(coins) {
if (coins[3] > 0) {
$('.quarter').html(coins[3] + " quarter(s).");
}
if (coins[2] > 0) {
$('.dime').html(coins[2] + " dime(s).");
}
if (coins[1] > 0) {
$('.nickel').html(coins[1] + " nickel(s).");
}
if (coins[0] > 0) {
$('.penny').html(coins[0] + " pennies.");
}
}
makeChange(total, coins);
showChange(coins);
However, this seems awfully repetitive and I'm finding that with certain values, it's a penny off. How can I make it more accurate and concise?
I'm finding that with certain values, it's a penny off.
Probably due to floating-point issues. And you shouldn't use parseInt to convert a number - it's meant for strings.
this seems awfully repetitive
A loop, with a data structure that represent the different coins will help. You already did something like that for your result: coins is an array, not 4 different variables.
function makeChange(total, values) {
var coins = [],
epsilon = 1e-5; // this is wrong in general!
// assume values are ascending, so we loop backwards
for (var i=values.length; i--; ) {
coins[i] = Math.floor(total / values[i].val + epsilon);
total %= values[i].val;
}
return coins;
}
function showChange(coins, values) {
for (var i=values.length; i--; ) {
var el = $(values[i].sel);
if (coins[i] > 0) {
el.html(coins[i] + " "+values[i].name+".");
} else {
el.empty();
}
}
}
var values = [
{val:0.01, sel:'.penny', name:"pennies"},
{val:0.05, sel:'.nickel', name:"nickel(s)"},
{val:0.10, sel:'.dime', name:"dime(s)"},
{val:0.25, sel:'.quarter', name:"quarter(s)"}
];
showChange(makeChange(parseFloat($('#total').val()), values), values);
Your best bet to avoid rounding problems is to just multiple your total by 100 to convert your dollar amount into all pennies, then do you conversion. For example:
function makeChange(total, coins) {
var remainder = 0;
total = Math.round(total * 100);
coins[3] = Math.floor(total / 25);
remainder = total - coins[3] * 25;
total = remainder;
coins[2] = Math.floor(total / 10);
remainder = total - coins[2] * 10;
total = remainder;
coins[1] = Math.floor(total / 5);
remainder = total - coins[1] * 5;
total = remainder;
coins[0] = total;
}
http://jsfiddle.net/t14cwdph/4/
For making your code easier to manage - see #Bergi's answer.

Categories