This question already has answers here:
JavaScript: How to reverse a number?
(19 answers)
Closed 2 years ago.
So i am trying to reverse an integer and so far the code works but i am trying to find a solution to reverse the integer without converting into a string first? Any help would be appreciated. This is my code snippet so far to reverse the integer.
function reverseInt(num) {
const reversed = num.toString().split('').reverse().join('')
return parseInt(reversed) * Math.sign(num)
}
console.log(reverseInt(-500));
I am trying to do it using javascript.
Try this:
function reverseInt(number) {
var isNegative = number < 0 ? true : false;
if(isNegative)
number = number * -1;
var reverse = 0, lastDigit = 0;
while (number >= 1) {
reverse = Math.floor(reverse * 10 + (number % 10));
number = number / 10;
}
return isNegative == true ? reverse*-1 : reverse;
}
console.log(reverseInt(-500));
console.log(reverseInt(501));
Related
This question already has answers here:
How to get numeric value from a prompt box? [duplicate]
(6 answers)
Javascript: "+" sign concatenates instead of giving sum of variables
(4 answers)
Adding two numbers concatenates them instead of calculating the sum
(24 answers)
Closed 2 years ago.
I am trying this code to add a number starting from 1 to n using this mathematical equation:n = n(n+1) /2
var n = prompt("enter a number");
function adding(n) {
let N = n * (n + 1) / 2;
return N;
}
alert(adding(n));
but it's not working in prompt. It works like this:
function adding(n) {
let N = n * (n + 1) / 2;
return N;
}
console.log("answer", adding(100));
This question already has answers here:
Reverse decimal digits in javascript
(14 answers)
Closed 6 years ago.
I trued to use my code that was written in c++ to output reversed number with while loop and i got output of "Infinity"
Can somebody explain why it happened and is there any other method to make it with loop instead of split().reverse().join()
Here is my code:
var n = 352, reverse = 0, remainder;
while (n>0) {
remainder = n%10;
reverse = reverse * 10 + remainder;
n = n / 10;
}
console.log(reverse);
The only missing term is rounding of the number to nearest integer.
Here is updated code.
var n = 352, reverse = 0, remainder;
while (n>0) {
remainder = n%10;
reverse = reverse * 10 + remainder;
n = Math.floor(n / 10);
}
console.log(reverse);
Use:
n = parseInt(n / 10);
instead of
n = n / 10;
This question already has answers here:
What is the fastest factorial function in JavaScript? [closed]
(49 answers)
Closed 7 years ago.
I'm learning Java Script and there is an exercise about getting the factorial of number user has entered but for some reason I always get answer is = 1
here is my code :
<SCRIPT>
function factorial(num){
for (n=1; n<=num; n++){
return fact*n;
}
}
var myNum, fact;
myNum = parseFloat(window.prompt('Enter positive integer : ',''));
fact = 1;
document.write('the factorial of the number is = '+ factorial(myNum));
</SCRIPT>
The pictured code (please include actual code in the future, not screenshots of code) returns fact immediately:
for ( n = 1; n <= num; n++ ) {
return fact * n;
}
since n starts at 1.
What you want is to include fact in the function, and multiply it as the loop goes along, then return:
function factorial(n) {
var fact = 1;
for ( n = 2; n <= num; n++ ) {
fact = fact * n;
}
return fact;
}
The reason this is returning 1 is because in your loop above, you return the value on the very first iteration, so n is never greater than 1.
This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
Closed 8 years ago.
I have a ten digit number that will always be constant. I want to pad it so, that it will always remove a zero for every extra number added to the number. Can someone please show me an example of how I can do this?
eg. 0000000001
0000000123
0000011299
You can use this function:
function pad (str, max) {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
Output
pad("123", 10); // => "0000000123"
JSFIDDLE DEMO
Just try with:
function zeroPad(input, length) {
return (Array(length + 1).join('0') + input).slice(-length);
}
var output = zeroPad(123, 10);
Output:
"0000000123"
Another variant would be:
function pad(input, length) {
return Array(length - Math.floor(Math.log10(input))).join('0') + input;
}
var out = pad(23, 4); // is "0023"
This question already has answers here:
How to format a number with commas as thousands separators?
(50 answers)
Closed 8 years ago.
I'm trying to add a comma between my 20000 to show as 20,000 without it messing up and have gotten pretty close but trying to fill in the next steps. Below, I've listed my code with the function thats able to do it but trying to connect the two together to properly work.
Here is my jsFiddle!
And code..
HTML
<span id="liveNumbers">23000</span>
JS
setInterval(function(){
random = (Math.floor((Math.random()*2)+1));
var plus = Math.random() < 0.5 ? 1 : 1;
random = random * plus;
currentnumber = document.getElementById('liveNumbers');
document.getElementById('liveNumbers').innerHTML = parseInt(currentnumber.innerHTML) + random;
}, 3000);
function commaSeparateNumber(val){
while (/(\d+)(\d{3})/.test(val.toString())){
val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
}
return val;
}
You'd have to change your initial value to include a comma or do an initial run before the setInterval is started but something like this might work:
setInterval(function(){
random = (Math.floor((Math.random()*2)+1));
var plus = Math.random() < 0.5 ? 1 : 1;
random = random * plus;
currentnumber = document.getElementById('liveNumbers');
var curnum = parseInt(currentnumber.innerHTML.replace(",",""));
document.getElementById('liveNumbers').innerHTML =
commaSeparateNumber(curnum + random);
}, 3000);
function commaSeparateNumber(val){
while (/(\d+)(\d{3})/.test(val.toString())){
val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
}
return val;
}