convert number to the nearest multiple of 10 - javascript

in javascript, lets say I got a random number 136, i'd like it to convert it automatically to 140 or if I got 124 to 120 etc or 24 to 20 etc..

Divide by 10 and round it, then multiply by 10.
var x = 136;
console.log(Math.round(x / 10) * 10);

I would just do this: ( Math.round(YourNumber/10) ) * 10

Related

How can run JavaScript code for all users at the same time and display the same generated numbers for all users

I have a big problem in my site.
I have in my site a page to generate a random numbers with Spinning Wheel in Javascript.
Code of generating numbers:
function chooseNumbers ()
{
const wheel = document.querySelector('.circle');
let deg = 0;
deg = Math.floor((Math.random() * (360 - 1) + 1));
wheel.style.transition = 'all 10s ease-out';
wheel.style.transform = `rotate(${deg}deg)`;
/**
* Circle height: 830px
* Circle width: 830px
* angle: 36deg
*
* if deg result between this numbers
*
* 0 => 36 = 1
* 36 => 72 = 2
* 72 => 108 = 3
* 108 => 144 = 4
* 144 => 180 = 5
* 180 => 216 = 6
* 216 => 252 = 7
* 252 => 288 = 8
* 288 => 324 = 9
* 324 => 360 = 0
*
* pi = c / d
*/
}
I need to display on my users "IN THE SAME TIME" how the page generate the numbers and how the Spinning Wheel choose the numbers.
That's mean I need to run the same Javascript Code and show the same results to all existing users.
If the page runs JavaScript code for every user
It will show different results for each user because I use
Math.random()
to generate the random number.
So I need a way to display the wheel spin to all users at the same time and display the same results that the code generates in JavaScript.
What is the necessary technique to do this method?
You could use a database for this. Theres free ones that you could use to try.
The working principle would be, with a different page, in javascript you generate various random numbers and their hour for them to be displayed and then you upload them using PHP to your database. That would be called your backend which is the part the user never gets to see. Now, for each user to see those numbers, you would start the spinning wheel and get the number from the database with PHP too, and then make the wheel stop at that number, in that way it'll the same number for each user and the wheel will keep as long as you keep adding numbers to your database

Calculate percentage increase using math formula

I have a function that calculates percentage increase of 2 numbers:
const 1st_num = 50
const 2nd_num = 100
percentage = ((1st_num - 2nd_num) / 1st_num) * 100 // -100
It seems correct but what if the 1st number is 1?
((1 - 50) / 1) * 100 // -4900
I don't see it making sense anymore. What am I missing?
If you are computing a delta variation in percentage between 2 numbers, it should be the other way around:
variation = ((num2 - num1) / num1) * 100
Last but not least, your delta can be over 100%
For example, imagine at
t1=10 and t2=11 -> your delta will be computed like this : (11 - 10)/10, so you have an increase of 10%
but if you have t1=10 and t2=100 -> your delta will become (100 - 10)/10, so you have an increase of 900%
First up all your question is more suitable to somewhere in math forums:
Your formula is right just change it as follows to get increase change in positive numbers:
percentage = ((2nd_num - 1st_num) / 1st_num) * 100 // 100%
However your treatment with 1 is exactly right.
4900 % In other words 49 times increase in value.
You can't use variable name starts with numbers
const fst_num = 50
const snd_num = 100
percentage = ((snd_num -fst_num) / fst_num) * 100
const fst_num = 70192.32
const snd_num = 17548.08
const percentage = ( 100 - ( ( fst_num - snd_num ) / fst_num ) * 100 );

How to Round Up an Integer Javascript

I would like to round up an integer with Javascript.
I have a JSON that retrieve an amount e.g 7435 but I want to round it up to 7500, so I can use in an simple math function. Any ideas? Cause round, and ceil are working only with decimals.
You can use the Math.ceil function
Math.ceil(7435/100)*100;
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/ceil
You can use this formula:
x = Math.floor((x+99) / 100)*100;
or
x = Math.ceil(x/100) * 100;
or go learn some Math :-)
Math.ceil( number / 100 ) * 100 should work. If you do Math.ceil(7435/100) it will give you 75 and multiplying by 100 will give you 7500. Ceil does not only work with decimals.
var rounded = Math.ceil( 7435 / 100) * 100;
console.log(rounded);

Javascript Tag cloud font size rolling over after 10 occurrances

I have a JSON string from my DB that gets fed into the script below, your basic tag cloud with font size being increased based on frequency. My problem is that once a frequency hits ten (10), the font size goes back to the same as if it were one. I've had similar problems where I was passing a string in PHP instead of INT, but that is not the case here.
{"tags":[{"tag":"programming","freq":11}]}
$(function() {
$.getJSON("tagcloud.php", function(data) {
$("<ul>").attr("id", "tagList").appendTo("#tagCloud");
//create tags
$.each(data.tags, function(i, val) {
//create item
var li = $("<li class=\"tagInfo\">");
//create link
$("<a>").text(val.tag).attr({class:"a",href:"index.php?page=tagdata&tagName=" + val.tag }).appendTo(li);
/*If I set the first equation to more than what my frequency is (10000 here for caution), it works fine.
But the equation in the line commented out where `(val.freq / 10 < 1) is not
giving me the correct output. Am I missing ensuring this variable is an
INT in my JS after it's retrieved in JSON? */
li.children("a").css("fontSize", (val.freq / 10000 < 1) ? val.freq / 10 + 1 + "em": (val.freq / 10 > 2) ? "2em" : val.freq / 10 + "em");
//li.children("a").css("fontSize", (val.freq / 10 < 1) ? val.freq / 10 + 1 + "em": (val.freq / 10 > 2) ? "2em" : val.freq / 10 + "em");
//add to list
li.appendTo("#tagList");
});
});
});
Try this (never mind the syntax changes -- it's just for readability):
li.children("a").css("fontSize",
((val.freq / 20) <= 1 ? (val.freq / 20) + 1 :
(val.freq / 10) >= 2 ? 2 : (val.freq / 10)) + "em");
Why...
Due to the initial use of less than < expression, rather than <=, your algorithm negates 10 / 10 as true and ends up in the "else" segment of your ternary statement (returning 1em).
Another issue your going to run into is that you've limited yourself to a very small range of values -- in this case, font sizes. Currently, your equation results in the following:
divisor | frequency | product | size (em)
-------------------------------------------
10 1 0.1 1.1
10 2 0.2 1.2
10 3 0.3 1.3
10 4 0.4 1.4
10 5 0.5 1.5
10 6 0.6 1.6
10 7 0.7 1.7
10 8 0.8 1.8
10 9 0.9 1.9
10 10 1.0 1.0
10 11 0.1 1.1
10 12 0.2 1.2
... ... ... ...
You can resolve the issue so that it will scale up, from 1.05em to 2em (with increments of .05em), by simply changing the divisor from 10, to 20. This will help to make sure that the size keeps scaling until it hits your threshold, 2em.
I would suggest marking up some of your variables as currently the size scales up for every 1 hit, and stops scaling at 20 hits. You know, 20 -> 200, 10 -> 100. Either way...

JavaScript - random multiple of 20 between 0 - 580

In Javascript, how do I create a random even number multiplied by 20 between 0 - 580?
E.g.: 220, 360, 180, 0 (min), 400, 200, 580 (max)
You want increments of 20, so what you really need is an integer in the range 0 to 29, and then multiply with 20. Example:
var max = (580/20) + 1;
var result = 20 * (Math.floor(Math.random())*max)
We are adding one to max, because Math.random() is a uniformly distributed number between (inclusive 0) and (exclusive 1), so since we use Math.floor, the maximum must be 1 larger.
This way creates a random number, then rounds it down to the nearest multiple:
When you need 0 <= randomMultiple <= max
var random = Math.random() * (580 + 20);
randomMultiple = random - (random % 20);
When you need 0 <= randomMultiple < max
var random = Math.random() * 580;
randomMultiple = random - (random % 20);
Use a principle like this: Generate random number between two numbers in JavaScript
Keeping in mind that if you want your max result to be 580, then the maximum integer you want to multiply by 20 would be 29 (or 580/20). Then just add some logic to make sure the integer is even.
Ta da!
Try use this:
var result = parseInt(Math.random()*30)*20;
29*20 = 580
Math.random() return [0..1)
result between 0..580, step by 20
Here is a generic javascript one-liner that can be used for any range, and any multiple.
Essentially, what we are trying to do here is figure out a range, 0 to N, which when multiplied by our given multiple stays within the range [0,max-min].
In this case N, is simply, (max - min)/multiple, or range/multiple.
Once we have N, we can use Math.random() to get a random number between 0-N, and multiply it with multiple. Next, we just add min.
We assume that min and max are already multiples of multiple.
Note the additional +1 to the input of Math.random() is because Math.random() returns a number between 0 (inclusive) and 1 (exclusive). So, Math.random() can never return 1. If we didn't account for that we would never be able to include the max number in our results.
/*
* Returns a random number within range [min,max]
*
* min and max must be multiples of multiple
* (note that 0 is a multiple of all integers)
*/
function randomMultiple (min, max, multiple) {
return Math.floor(Math.random() * (((max - min)/multiple)+1)) * multiple + min;
}
console.log(randomMultiple(0, 580, 20));

Categories