Multiple character for X number of times [duplicate] - javascript

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

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);

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]));
}

Display Random Number by Javascript [duplicate]

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...

Format float with two decimals without using .toFixed() [duplicate]

This question already has an answer here:
Javascript Adding Two Decimal Places
(1 answer)
Closed 9 years ago.
I have a float,
var a = 324620.8
and I want it to look like this
a = 324620.80
This is my code so far,
var a_float = a;
var a_int = parseInt(a);
d = a_float - a_int;
if(d <= 0){
a = a_int+'.00';
}else{
if(d < 0 && d > 0.1){
a = a_int + d + '0';
}else{
a = a_float;
}
}
This would works for only one decimal digit.
I want it to work when I have 2 decimal digits.
.toFixed would not work in some browsers.
Answering the question in the title
How to find how many decimal digits in a float?
Compare position of '.' to length of float as a String.
var x = 1.2345,
x_str = x.toString(),
decimal_digits = x_str.length - x_str.lastIndexOf('.') - 1;
decimal_digits === x_str.length && (decimal_digits = 0); // case no decimal
decimal_digits; // 4
JSFIDDLE
Use toFixed("2");
var f = 1.3454545;
f.toFixed(2);
var decimal = 4.0;
var a = (decimal).toFixed(2);
console.log(a); // outputs 4.00

Categories