This question already has answers here:
How to get numeric value from a prompt box? [duplicate]
(6 answers)
Javascript string/integer comparisons
(9 answers)
Closed last year.
Im trying to get the largest number among the numbers the user give me without using a array, this is how i have done it:
let largestNum = 0
let userNum = 0
for(let i = 1; i<=3 ;i++){
userNum = prompt("Give a number")
if(userNum > largestNum){
largestNum = userNum
}
}document.write("the biggest number is: "+ largestNum)
Now, if im playing the user and insert first 10 and then 2 and 2, it print me that the largest number is 2! how come is it 2?!
after the first time the largest number should turn into 10, and from then, no number is larger than 10! so how come it i gives me 2?
Related
This question already has answers here:
Javascript How to get first three characters of a string
(2 answers)
Keep only first n characters in a string?
(7 answers)
Closed 7 days ago.
I'm confused where I want to split the correspondence_code value like in the image below : let say I want to get just the first 5 digits like 16020 it is possible just getting the first 5 value? Thanks in advance
var result = objt['psgc'].filter(obj => {
return obj.geographic_level === "province"
})
// I want to split the value result -> correspondence_code with only first 5 digits
This question already has answers here:
Count the number of integers in a string
(6 answers)
Closed 4 years ago.
I currently have a text input in my HTML document and some buttons below it. When pressed, these will run a function input(some number). I have added a feature to the function that prevents the length of the string for the input from being above 5. However, sometimes the string (currentAnswer) will be set to something like -768.. When this happens, the function will not let you enter any more numbers, when instead you should be able to enter 2 more digits. So how would I find the length of just the numbers in a string? Here is my code:
function input(num) {
currentAnswer = document.getElementById("answerBox").value;
answerLength = currentAnswer.length;
if (answerLength < 5) {
document.getElementById("answerBox").value = currentAnswer + num;
answerLength = answerLength + 1;
}}
As you can see, I am using currentAnswer.length to get the length of the string. But I only want the amount of digits. Can someone please help me?
Count the number of integers in a string
alert("g66ghy7".replace(/[^0-9]/g,"").length);
:)
This question already has answers here:
Regex using javascript to return just numbers
(14 answers)
Closed 4 years ago.
Say I have the following string or one like it:
var sentence = "Hey, I got 3 apples today from the 2 food stalls I was told about by 4chan. I'll give you one in a minute or 20."
and I want the following array from it:
var num_array = some_magic_function(sentence); // Contains: {3, 2, 4, 20}
I was looking at splitting by spaces, but that won't get numbers blended in. I can't do number by number because if a number has multiple digits, that's a problem.
You can use a regex to handle this.
var num_array = sentence.match(/\d+/g).map(Number)
This question already has answers here:
Is there a way to round numbers into a reader friendly format? (e.g. $1.1k) [closed]
(2 answers)
1000000 to 1M and 1000 to 1K and so on in JS [duplicate]
(1 answer)
Closed 5 years ago.
I want to trim my 5 or more than 5 digit number 12345 to
12.34k
to fit it in my small box. Is there any util to use?
you can divide it by 1000 and then specify the number of digits you want using toFixed()
let x = 12345;
x = x/1000;
x = x.toFixed(2);
x = x + "k";
console.log(x)
This question already has answers here:
Create a string of variable length, filled with a repeated character
(12 answers)
Closed 5 years ago.
I'd like to display a number of blank letter spaces = to the number of letters of a random word I pull from an array.
This is for a hangman game for a class project. I have the randomly pulled word and the number of letters in that word but trying to use the variable that I've assigned that number too is proving a bit tricky.
Any help appreciated!
You can try the following code:
// The array of words you have
var $words = [
'Totidem',
'Pugnabant',
'Calidis',
'Circumfluus',
'Formaeque'
];
// Pick a random array index
var $idx = Math.floor( Math.random() * $words.length );
// Get the word in the index equal to $idx
var $word = $words[$idx];
// Generate the repeated string. In case you like to display a different
// character, replace the `.join(" ")` with the character you need. For
// example `.join("_")` or `.join("_=_")`
var $repeated_string = Array( 1 + $word.length).join(" ")