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));
Related
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));
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have an issue with the subtraction of two values. when I set discount_amt value to 2.5 then total return me 0.5 but when discount_amt is set to 2.6 it return 0.3999999999999999 instead of 0.4 why?
var total = parseFloat('3').toFixed(1);
var discount_amt = parseFloat('2.6').toFixed(1);
total = total - discount_amt;
console.log(total);
var total = parseFloat('3').toFixed(1);
var discount_amt = parseFloat('2.6').toFixed(1);
total = total - discount_amt;
console.log(total);
This seems to fix it. You forget parsefloat() and tofixed()
total = 3;
discount_amt = 2.6;
console.log(parseFloat(total).toFixed(1) + ' ' + parseFloat(discount_amt).toFixed(1));
total = parseFloat(total).toFixed(1) - parseFloat(discount_amt).toFixed(1);
console.log(parseFloat(total).toFixed(1));
Explanation why floats are handled this way: answer or directly to the link that answer refers to link
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:
Javascript roundoff number to nearest 0.5
(10 answers)
Closed 7 years ago.
I'm looking for an output of
4.33 = 4.5
4.5517263843648 = 5
Using the Math.round function in javascript. I've tried a few methods but neither seem to work.
Try to use,
function test(val) {
var x = Math.floor(val);
return (val - x) > 0.5 ? Math.ceil(val) : (x + 0.5);
}
DEMO