How do I get the amount of numbers in a string? [duplicate] - javascript

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

Related

Remove a certain character from a string in an array [duplicate]

This question already has answers here:
Remove a character at a certain position in a string - javascript [duplicate]
(8 answers)
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 18 days ago.
I'm trying to do a calculation with time and need to remove the ":" which splits hours and minutes.
My array currently holds a string value of "12:04"
I created a for loop to iterate through the second array string by length, check for a :, then remove that character and log the new output. However, my logic is not working as intended. If you can, please let me know what I did wrong so I can fix my issue.
for (let i = 0; i < content[2].length; i++) {
if (content[2].charAt(i) === ":"){
content[2].slice(i);
console.log(content[2])
}
}
If you are sure that ":" will appear only once, then keep it simple
content[2] = content[2].replace(":", "");
Full code:
const result = content.map(str => str.replace(":", ""))
I think this works well:
content.split(':').join('')
Here is the output I got from the console:
"12:04".split(':').join('')
'1204' // Output

get the largest number without an array [duplicate]

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?

Why does it show value 5150 and not 155? [duplicate]

This question already has answers here:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 4 years ago.
So im quite new to javascript and i tried making something simple as entering a value and add 150 to it but it wont show number + 150?
Pictures below
Code
Output
That's because you are concatenating strings. You need to convert the strings to integers by using parseInt(), and then add the numbers.
https://www.w3schools.com/jsref/jsref_parseint.asp
var fullprice = parseInt(price) + 150;
Because you're actually concatenating a string with a number. The input value is a string, so before operating with it, parse it to int with parseInt
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

(Js) number method to add a comma after the first number and remove the 2 number after comma ?? is there any? [duplicate]

This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 8 years ago.
I want to format a number to two decimal places. Say the user enters 8764444 it should be formatted as 8.76. is there some built-in function in javascript to do that?
No, there is no built in method for exactly that, but you can use the substr method to get parts of a string to do the formatting:
var input = "8764444";
input = input.substr(0, 1) + '.' + input.substr(1, 2);
// show result in Stackoverflow snippet
document.write(input);

javascript parseInt [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to parseInt a string with leading 0
If I parseInt("01") in javascript its not the same as parseInt("1")???
start = getStartEugene("MN01");
start2 = getStartEugene("MN1");
getStartEugene: function(spot) //ex: GT01 GT1
{
var yard = spot.match(/[0-9]+/);
var yardCheck = parseInt(yard);
if (yardCheck < 10)
return "this"+yard;
else
return "this0"+yard
}
I want something to be returned as this+2 digits such as this25, this55, this01, this02, this09
But i am not getting it. Anyone know why?
You need to add the radix (2nd) argument to specify you are using a base 10 number system...
parseInt("01", 10); // 1
This happens because Javascript interprets numbers starting with zero as an octal (base 8) number. You can override this default behaviour by providing the base in which the string will be evaluated (as #jondavidjohn correctly pointed).
parseInt("10"); // returns 10
parseInt("010"); // returns 8

Categories