Display Random Number by Javascript [duplicate] - javascript

This question already has answers here:
Generating random whole numbers in JavaScript in a specific range
(39 answers)
Closed 9 years ago.
I'm following:
window.onload = generateRandomNumber;
function generateRandomNumber(){
var n = 25;
var number = Math.floor(Math.random()*n)+1;
document.getElementById("randomNumber").innerHTML = number;
}
Now I want to show a random number that from 20 to 25. How can I do that?
Thanks!

Just try:
var n = Math.floor(Math.random()*5) + 1;
n += 20;
The n value will be between 20 and 25...

Related

Why doesn't my JavaScript program to find odd numbers work? [duplicate]

This question already has answers here:
Javascript string/integer comparisons
(9 answers)
Sum of two numbers with prompt
(10 answers)
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
Closed 1 year ago.
I made a simple js code to input start value and end value form prompt and then find all the odd numbers, unfortunately it's not working properly. when i input 1 and 10 it'll work, but when i input 5 for sValue(start value) the program won't work. any idea?
var odd = [];
var sValue = prompt("start");
var eValue = prompt("end");
for (var i = sValue; i <= eValue; i++) {
if (i % 2 != 0) {
odd.push(i);
}
}
alert(odd);
Because the value of prompt is a string. You need to convert it to a number with parseInt(v, 10).
var odd = [];
var sValue = parseInt(prompt("start"), 10);
var eValue = parseInt(prompt("end"), 10);
for (var i = sValue; i <= eValue; i++) {
if (i % 2 != 0) {
odd.push(i);
}
}
alert(odd);

Trying to solve Sum of Digits / Digital Root with JS but having issue, please can any one explain it [duplicate]

This question already has answers here:
Number with leading zeroes gets changed in JavaScript
(4 answers)
Why JavaScript treats a number as octal if it has a leading zero
(3 answers)
Closed 1 year ago.
solving Sum of Digits / Digital Root and facing problem in this case where input is 010 the output comes 8.
please can anyone explain?
function fun(n) {
let numString = n.toString().split("");
let sum = 0;
for (let i = 0; i < numString.length; i++) {
sum += parseInt(numString[i]);
}
console.log(sum);
if (sum >= 10) {
return fun(sum);
}
console.log(numString);
return sum;
}
console.log(fun(010)); // input- 010 and output- 8
Why the output is 8 but it should be 1.

How to round a variable value to 2 digit in javascript [duplicate]

This question already has answers here:
Pad a number with leading zeros in JavaScript [duplicate]
(9 answers)
How can I pad a value with leading zeros?
(76 answers)
Closed 5 years ago.
Suppose,
a=0;
then the result should be 00
a=10
result=10
a=2
result=02
like all values needs to round in 2 decimal point.
Note: No need to round the values having more than 2 digits.
Are you looking for something like that;
var int = 3;
var intStr = ("0" + int).slice(-2);
Output : 03
For any number of digits
var temp = 9;
if(temp < 10){
var temp = ("0" + temp).slice(-2);
}
For only two digit simply append zero if it is one digit number :-
var temp = 19;
if(temp < 10){
var temp = "0" + temp;
}

Number format with comma and decimal points [duplicate]

This question already has answers here:
How to format numbers as currency strings
(67 answers)
Closed 6 years ago.
I have tried using Number(x).toLocaleString(), but this only gives me 10,000.
When I use parseFloat(row.profit).toFixed(2) it gives me 10000.00. I tried combining parseFloat(Number(row.profit)toLocaleString()).toFixed(2) But not give me the desired output which should be 10,000.00.
How can I achieve this?
You can use a quick hack by testing if . is present in your locale string or not :
function localeFormat(x) {
var num = Number(x).toLocaleString();
if (num.indexOf("/.") > 0) {
num += ".00";
}else{
var n = parseFloat(x).toFixed(2).toString();
num = Number(n).toLocaleString();
}
return num;
}
var strs = ["10000", "10000.45", "10000.45768"];
for(var i = 0; i < strs.length; i++){
console.log(strs[i] + " -> " + localeFormat(strs[i]));
}

Multiple character for X number of times [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Repeat Character N Times
For example:
var x = 5,
char = 'F';
// here return 'FFFFFF'
How can I do that ?
var x = 5;
var c = 'F';
return Array(x + 1).join(c);
From Repeat Character N Times

Categories