In the code below, how does the code know to do something like this, if n = 8:
8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
instead of doing * 0 at the end? Does the counting variable (n, or any counting variable ever) never hit 0 when using n--?
var n = document.getElementById("selNumber").value;
var result = 1;
while(n) {
result *= n;
n--;
}
Because when n = 0, the 0 is interpreted as false and the loop is never entered.
Related
Recently i started to learn recursion in JavaScript.
i never called defined function in its own function.
what is this called?
i'm unable to understand how this recur(n - 1) working.
i tried to check how it working in chrome debugger, but there is no output too.
How here recur(n - 1) working?
function recur(n) {
if(n === 1) return 1
return n * recur(n - 1);
// How here recur(n - 1) working?
// 5 * 5 - 1 = 24
// 4 * 4 - 1 = 15
}
recur(5);
So when you do return n * recur(n - 1), the first thing that is calculated is recur(n - 1).
What happens is the following
recur(5) = 5 * recur(5 - 1)
recur(4) = 4 * recur(4 - 1)
recur(3) = 3 * recur(3 - 1)
recur(2) = 2 * recur(2 - 1)
recur(1) = 1 => the bottom of the recursion. Now it starts to go back:
recur(2) = 2 * recur(1) = 2
recur(3) = 3 * recur(2) = 3 * 2 = 6
recur(4) = 4 * recur(3) = 4 * 6 = 24
recur(5) = 5 * recur(4) = 5 * 24 = 120
Basically, until a function of recur(n - 1) returns something concrete, like the bottom of the recursion in your case 1, the method calls recur once again, and again and so on. After you go to the bottom, then the functions start to resolve in the reverse order.
The recursive function is calculating the factorial of n.
The function will call itself until it reaches a base case.
You have one base case: when n is 1 then return 1.
If you call it with say 5 then it works as follows:
recur(5) =>
5 * recur(4) =>
5 * 4 * recur(3) =>
5 * 4 * 3 * recur(2) =>
5 * 4 * 3 * 2 * recur(1) =>
5 * 4 * 3 * 2 * 1 =>
120
So the result will be 120 for n=5.
please someone explain this to me
function func(x, n) {
if (n == 1) {
return x;
} else {
return x * func(x, n - 1);
}
}
console.log(func(2, 4)); // 16
how do answer is 16
I could not understand more than this
when if is false it going to else
in the return section function call itself
at the first input is the same value as x, ie 2
at the second input is n - 1
when function run again the value of x remains the same as 2 and the value of n becomes 3
that's mean 3 times if is false
in the 4 times if if true and the execution loop ends and show answer in alert
i can not figure out what number is multiplied by x per run or the answer should be the same as 2
because every time the function is executed, shouldn't the value of x be equal to 2?
If you are asking about how recursion works, then I will explain you with this basic flowchart diagram.
I also edited your JS Code so it will provide you with clarification.
function func(x, n) {
if (n == 1) {
return x;
} else {
let function_returned = func(x, n - 1);
console.log(x, "*", function_returned, "=", x*function_returned)
return x * function_returned
}
}
console.log(func(2, 4));
The function performs exponentiation. So func(x, n) is calculating xn.
It uses recursion, based on the following principle:
When n is 1, then xn = x1 = x
When n > 1, then xn = x.xn-1
For the second statement we can use the function with arguments x and n-1.
For the concrete example, func(2, 4), this is what happens:
x=2, n=4, so we get in the else block and evaluate:
2 * func(2, 3)
For this nested call of func we have x=2, n=3, and again we get in the else block for that:
2 * func(2, 2)
And again we have a nested call, which evaluates to:
2 * func(2, 1)
...and this final nested call gets into the if block and evaluates to:
2
Now we need to backtrack as each nested function call returns the result to its caller:
2 is returned for func(2, 1) which turns 2 * func(2, 1) to 4
That 4 is returned for func(2, 2) which turns 2 * func(2, 2) to 8
That 8 is returned for func(2, 3) which turns 2 * func(2, 4) to 16
And that 16 is what you get in the output
It's a recursive power function. What it does is the following:
2^4 = ?
2 * 2^3 = ?
2 * 2 * 2^2 = ?
2 * 2 * 2 * (2^1) = ? // at this point we get to n == 1 and this is where result is just x which is 2 in your example. From this point we can start moving "back"
2 * 2 * (2 * 2) = ?
2 * (2 * 4) = ?
(2 * 8) = ?
16 = ? // 16 is just 16, we're done.
Your can read a bit about recursion to get a better idea on what is going on.
This javaScript code block is making me crazy,that how it is doing the computation behind. I try to manually do it with calculator but the results start to be different at 4. Please explain if you know how
function fac(n) {
if (n == 0)
return 1;
else
return fac(n - 1) * n;
}
if i run console.log(fac(1));
// > 1
if i run console.log(fac(2));
// > 2
if i run console.log(fac(3));
// > 6
i was thinking this is what was happening behind
(2-1) * 2 = 2,
(3-1) * 3 = 6,
until i put
(4-1) * 4 = 12.
if i run console.log(fac(4)); the output is 24 not 12
// > 24 how and why?
cheers
It is performing recursive function call to the same function.
function fac(n) {
if (n == 0)
return 1;
else
return fac(n - 1) * n;
}
fac(4);
Value of n is 4. So it should returnfac(4 - 1) * 4). Now fac(4 -1) is calculated before returning the value.
Value of n is 3. Now it will return fac(3 - 1) * 3), which replaces fac(4 - 1) in previous statement. So, it will become fac(3 - 1) * 3 * 4.
Value of n is 2. Now it will return fac(2 - 1) * 2), which replaces fac(3 - 1) in previous statement. So, it will become fac(2 - 1) * 2 * 3 * 4.
Value of n is 1. Now it will return fac(1 - 1) * 1), which replaces fac(2 - 1) in previous statement. So, it will become fac(1 - 1) * 1 * 2 * 3 * 4.
Value of n is 0. Now it will return 1, which replaces fac(1 - 1) in previous statement. So, it will become 1 * 1 * 2 * 3 * 4.
As you can see, you can avoid multiplying by 1 twice by changing your code to
function fac(n) {
if (n == 1)
return 1;
else
return fac(n - 1) * n;
}
In short,
function fac(n) {
return (n == 1) ? 1 : (fac(n - 1) * n);
}
That's a factorial. It's calculates all permutation a set with n elements has. Or simpler:
[factorial] is the product of all positive integers less than or equal to n.
Wikipedia:Factorial
It's noted as n!. For example:
1! = 1
2! = 1 * 2 = 2
3! = 1 * 2 * 3 = 6
4! = 1 * 2 * 3 * 4 = 24
What the function is doing is recursively calling itself. When you execute the function fac(4) the following happens:
At fac(4), since 4 != 0 the value returned is fac(4 -1) * 4. However, since the return function contains a reference to a function, we wait for it to return a value.
At fac(3) (i.e. fac(4-1)), since 3 != 0 the value returned is fac(3-1) * 3. However same thing happens and we wait for result of fac(3-1)/fac(2).
At fac(2) (i.e. fac(3-1)) since 2 != 0 the value returned is fac(2-1) * 2. Again, since we are calling a function we wait for its return until we proceed will calculation.
At fac(1) (i.e. fac(2-1)) since 1 != 0 the value returned is fac(1-1)* 1 and we defer for fac(1-1) to return result.
At fac(0) (i.e. fac(1-1)) since 0==0 we return value one which is neutral for multiplication.
In fac(1), fac(1-1) becomes one, so fac(1) value becomes 1*1 or 1.
In fac(2), fac(2-1) is replaced with 1 so fac(2) becomes 1*2 or 2.
In fac(3), fac(3-1) is replaced with 2, and fac(3) becomes 2*3 or 6.
In fac(4), fac(4-1) is repalced with 6 and fac(4) becomes 6*4 or 24.
var a = 0;
(++a)+(a++)+(++a);
print(a);
This prints 3. I'm assuming it only executes single increment.
var a = 0;
(++a)+(a++)+(--a);
This prints 1. What's the rule to follow here?
Thank you.
You're not assigning the outcome of your addition to anything. You do this:
(++a)+(a++)+(++a);
Which increments a 3 times. 0 + 3 = 3 so a is the value 3.
JavaScript is executed left-to-right. You can see this by seeing what happens when you use multiplication
a = 1;
++a * a; // 4
// 2 * 2 = 4
a = 1;
a * ++a; // 2
// 1 * 2 = 2
a = 1;
a++ * a ; // 2
// 1 * 2 = 2
a = 1;
a * a++; // 1
// 1 * 1 = 1
After each of these, the resulting a is 2.
var x = 1 + Math.Random() % 9;
if (x==1)
// do something
else if (x==2)
// do something else
I used this line — (1 + Math.Random() % 9) — in C++ to get a number between 1 and 9, but in JavaScript I'm getting a different result.
Math.random() returns a value between 0 and 1, so instead using the modulo operator you need to use a multiplication.
1 + (Math.random() * 9);
Finally, you should round or .floor() that value
var x = Math.floor( 1 + ( Math.random() * 9 ) );
or, shorter
var x = ~~( 1 + ( Math.random() * 9 ) );
There is no Math.Random() function in JavaScript. It's Math.random(). Note the capitalization.
To get a random number between a certain minimum and maximum value, do this:
var min = 1, max = 9;
Math.floor(Math.random() * (max - min + 1)) + min;
Further reading: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/random
In Javascript the Math.random function returns a number between 0 and 1. If you want to get a number between 1 and 9 you'll have to work with it a bit.
var number = ((Math.random() * 10) | 0) % 9 + 1
This will give you a result between 0 and 9
Math.floor(Math.random()*9)
And by the way, jQuery is a javascript framework. Math is a native javascript function