How to alway show 3 digit on slotmachine jquery [duplicate] - javascript

This question already has answers here:
JavaScript format number to day with always 3 digits [duplicate]
(6 answers)
Closed 4 years ago.
I have a problem in my slot machine,
How to make it always display 3 digit
For example, I random the number 001 - 200
I want it to show 001 from start
and I want to display number like this 002 010 067 when it stop random
$('.reel-container:first').slotMachine(001).toString();
$('#gen').click(function() {
$('.reel-container:first').slotMachine((Math.floor(Math.random() * 200) + 1).toString());
});
My jsfiddle https://jsfiddle.net/xmenzaa/mrs93b58/11/
Thanks

use this function to generate random number
function randGen(){
var randNum = (Math.floor(Math.random() * 200) + 1).toString()
if(a.toString().length==3){
return randNum;
}
else if(a.toString().length==2){
return "0"+ randNum;
}
else if(a.toString().length==1){
return "00"+ randNum;
}
}
this code generate 3 digit random number and use it in
$('.reel-container:first').slotMachine(randGen());

Related

Why this code goes to concatenation and not adding? [duplicate]

This question already has answers here:
How to make 1 + 1 = 2 instead of 1 + 1 = 11 [duplicate]
(5 answers)
Closed 3 years ago.
I'm trying to do a function which add 5 each time I'm adding character to textarea, and subtract 5 when I'm removing a character.
This is my HTML
<textarea rows="4" cols="50" id="my-area"></textarea>
<p>Chars: <span id="counter">0</span></p>
This is my JS
let area = document.getElementById("my-area"),
counter = document.getElementById("counter"),
count = 0;
area.onkeyup = area.onkeydown = area.onpaste = area.onchange = function() {
if (area.value.length > count) {
count++;
parseInt(counter.innerHTML);
counter.innerHTML = parseInt(counter.innerHTML + 5);
} else if (area.value.length < count) {
count--;
counter.innerHTML -= 5;
}
};
When I add a character first time it works correct - 5 is adding to innerHTML of counter, but when I add second character number is concatenated not added. Subtraction works good. How Can I fix the adding?
And the second situation: How to make it work if I add to p which id is "counter" a string "PLN" or "DOLLARS" (example: "0 PLN") and I want it to add five to the number and substract 5 after?
Its because of the Type Coercion in javascript.
Instead parse it to int and then add 5
parseInt(counter.innerHTML) + 5

Random number out of three number in js [duplicate]

This question already has answers here:
Javascript function to generate random integers with nonuniform probabilities
(3 answers)
Closed 4 years ago.
Hi I wanna get random numbers out of 2,3,5 . So i use this code.
function Random(low, hi) {
return Math.floor(Math.random() * (hi - low + 1) + low);
}
But this is only for 2 numbers , But i wanna get for 1 time for example 3 for 2 time 5, and 3 time 3 , but of course not exactly in that order . Any ideas ?
function random(numbers) {
return numbers[Math.floor(Math.random()*numbers.length)];
}
Then you can call it with a list:
random([2, 3, 5])

Rounding a Number variable in javascript [duplicate]

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

JavaScript check if number is whole [duplicate]

This question already has answers here:
How do I check that a number is float or integer?
(52 answers)
Closed 7 years ago.
im trying to check if a number is a whole after a calculation. What I have so far prints out how many times one number gets divided by another, but when the number is not whole it dose not print anything out. Heres my code:
function round() {
var percent = document.getElementById('percent_sale').value;
var perShare = document.getElementById('singleShare').value;
var result = (percent / perShare);
if(result % 1 == 0) {
document.getElementById('results1').innerHTML = ('Number of shares:'+result);
} else {
document.getElementById(results1).innerHTML = ('number of shares must ');
}
}
The values get input buy a user, and the percent for sale is say 50 and the single share is say 2.5 this would return 20 shares.
What I need is if I put in something like 50 for sale and 3.15 single share it tells the user to make equal number of shares as it would return 15.87
Any ideas where ive gone wrong?
Convert your number into string and then check if the string contains only numbers
var num = 15;
var n = num.toString();
This will convert it into string then this
String.prototype.isNumber = function(){return /^\d+$/.test(this);}
console.log("123123".isNumber()); // outputs true
console.log("+12".isNumber()); // outputs false
For further reference.Link StackOverFlow

Format a number to a string in javascript [duplicate]

This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
Closed 9 years ago.
I can't figure out how to solve the following problem.
I have an array of numbers from 1 to 100.
I need to convert them to strings but to a length of 5.
So, for instance:
1 becomes 00001
2 becomes 00002
3 becomes 00003
4 becomes 00004
etc, etc..
It seems so easy but I cannot find a function. The best I found was .toFixed(n) which is the number of decimal points to use.
Here's a very simple padding function:
function padLeft(str, length, paddingCharacter) {
str = '' + str; //Make sure that we convert it to a string if it isn't
while (str.length < length) {
str = paddingCharacter + str; //Pad it
}
return str;
}
padLeft(123, 5, '0'); //00123

Categories