This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 6 years ago.
So I'm trying to create a random number generator in html using javascript to randomise and alert the user of the number generated. However I always get an error when getting my number which I don't experience when using codecademy. The code is:
<input type="button" value="Enter" id="enter"
onClick="alert(getRandomInt(document.getElementById('min').value,
document.getElementById('max').value));">
</div>
<script type="text/javascript">
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
When 5 and 10 are input as min and max numbers respectively,for example,the resulting numbers are 15,25,05,35 etc. The first digits apparently is found using the function
Math.floor(Math.random() * (max - min + 1))
and the last digit would be the number 5 which is the minimum. So I was wondering if anyone could teach me how to put the + min into the entire operation so I can get a number within the range of 5-10? Thanks in advance
min is a string and by adding, you add a string, with subtraction the value is casted to Number.
You could add another + sign in front of min, that cast the value to Number
return Math.floor(Math.random() * (max - min + 1)) + +min;
// ^^^
Related
This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(40 answers)
Closed 1 year ago.
So basically i got this function
Math.random()
we were taught to use this formula in order to get values from 1 to x
parseInt(Math.random()*x)+1;
but now what i want to do is to get values from x to y but i'm not sure how to do it .. any help ?
const doRandom = (min,max) => {
return Math.floor(Math.random() * (max - min) + min);
}
console.log(doRandom(5,9));
This question already has an answer here:
1000000 to 1M and 1000 to 1K and so on in JS [duplicate]
(1 answer)
Closed 1 year ago.
On my site, I've got a custom field on the backend of posts where I can enter a number, in this case, revenue. It's entered as a raw number, like 1000000. There are some numbers that go as high as one billion. I'd like to add a code somehow that lets the front end number appear as $1M or $1B instead of the long-form of the number. Is there a code I can create/use to help me do this? And would it go into the css or the html? It's important that the number on the backend remain a raw number, because I've got post filtering set up based on those numbers.
This should work, the real price remains in the variable so you can work with it later
let price = 3030000;
if (price >= 1000000) {
document.getElementById("price").innerHTML = "$" + (price / 1000000) + "M";
}else if (price >= 1000) {
document.getElementById("price").innerHTML = "$" + (price / 1000) + "K";
}else {
document.getElementById("price").innerHTML = "$" + price;
}
<p id="price"></p>
var damage = Math.max(Math.floor(Math.random() * max)+1, min);
What is the meaning first Math.max and last ,min here. Please explain the whole line.
Math.max is a function that, when passed a variable amount of numbers, returns the largest number:
console.log(Math.max(1, 2, 3));
min in your code is the minimum number that damage can be - so it works out as (pseudocode):
set damage to the maximum number out of:
A random number between 0 and 1, multiplied by max, rounded down,
or min;
Math is a build in object in javascript that has method and properties & here [Math.max][2] is use to find the largest of numbers. Math.floor & Math.random are also available methods
I have broken down the code into multiple lines, for ease of understanding
function findMax(max, min) {
let findRandom = Math.random() * max; // generate a random number
// if it is a decimal number get the previous whole number
let findLower = Math.floor(findRandom);
// add one with the generated whole number
let addOne = findLower + 1;
// find the maximum between two numbers, one is generated & another is
// supplied as argument
let damage = Math.max(addOne, min)
return damage;
}
console.log(findMax(4, 1))
If you look up Math.random at MDN:
The Math.random() function returns a floating-point, pseudo-random
number in the range 0–1 (inclusive of 0, but not 1) with approximately
uniform distribution over that range — which you can then scale to
your desired range.
So here, the range is max and you are taking the Math.floor of the expression Math.random() * max and then adding 1 to it.
Math.floor will round the number produced to the largest integer less than or equal to a given result. So if the Math.random() * max results in say 5.95 the Math.floor will make the resulting number 5.
Then at the end we are finding the maximum of the resulting number of the previous step and the min variable and assigning the result to the damage variable.
This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 6 years ago.
I have this code:
function sellByte() {
if (player.bytes >= 1) {
player.bytes = player.bytes - 1;
player.money = player.money + 0.10;
document.getElementById("bytes").innerHTML = "Bytes: " + player.bytes;
document.getElementById("money").innerHTML = "$" + player.money;
}
}
And whenever I sell a Byte my money value ends up looking like $10.00000003 or something along those lines, how would I go about rounding the money value UP every time this function is run?
Working with float numbers in JS is very tricky. My suggestion is to operate only with smaller units (cents instead of dollars) and then you will only deal with integers and will not have similar issues.
Use Math.round(player.money* 100) / 100 for 2 decimal rounding.
Use any of the following code
Math.round(num * 100) / 100
using fixed Method
var numb = 123.23454;
numb = numb.toFixed(2);
or you can refer following link for more help
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
This question already has answers here:
How to deal with big numbers in javascript [duplicate]
(3 answers)
Closed 8 years ago.
I need to generate a 12 digit and 13 decimals float value like this:
123438767411.238792938
How can I do that in Javascript/Jquery? Is it possible to generate this code using JavaScript?
I was trying like this:
v1 = Math.floor((Math.random()*10000000000)+1);
v2 = Math.floor((Math.random()*100000000)+1);
v = v1.toString() + "." + v2.toString();
But this is not working!
(assuming you mean in the form of a string, not as a number, because IEEE 754 can't have that many significant digits)
must the integer part be 12 digits or can it be 1 or 123? If it can be 12 digits or shorter, then it can be
(Math.floor (Math.random() * Math.pow(10,12))
+ (Math.floor (Math.random() * Math.pow(10,13))
/ Math.pow(10,13)).toString().substring(1))
note that the above could have an issue when the decimal part turns out to be 0, although the chance is really small. (then the .0 part is gone, although we can use a conditional to add it when it is so). Or we can treat the decimal part 123 not as .0000000000123 but as .123 and use:
(Math.floor (Math.random() * Math.pow(10,12))
+ "." + Math.floor (Math.random() * Math.pow(10,13)))
But it depends whether we care about 123 becoming .123 and 1230 also becoming .1230 because if we do care about it, we can say .123 is the same as .1230.
Also, if we want to have the form such as 000042987017.0790946977900 as well, so that it is always 12 digit integer and 13 digit decimal, then either we can do zero padding or use something like this:
sample: http://jsfiddle.net/jL4t4/1/
var i, s = "";
for (i = 0; i < 26; i++) {
s += (i === 12) ? "." : Math.floor(Math.random() * 10);
}