how to understand javascript for loop? [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this simple javascript code:
function power(base, exponent) {
var result = 1;
for (var count = 0; count < exponent; count++)
result = result * base;
return result;
}
power(2, 10);
but it s hard for me to understand what is the relationship between for loope and the result in the for loop. Can anyone describe the process in here? I know that the answer is 1024 and that it is 2*2*2*2*2*2*...... but it is har for me to understand how for and result is related. result is always 1? Or somehow it grabs the updated version from the loop? Thank you!

Say I pass power(2, 10). Here's how it's going to run, with each iteration:
i | result
--+-------
- | 1
0 | 1 * 2 = 2
1 | 2 * 2 = 4
2 | 4 * 2 = 8
.
.
.
9 | 512 * 2 = 1024
However, the numbers array is redundant. You'll need to check against exponent (i < exponent)

numbers is totally unnecessary. the length of the array [0....x] is x. So your for loop is really for (var i = 0; i < 10; i++). Hope that sorts it out. Also, you don't want to go to 10, but rather to exponent. Try:
for (var i = 0; i < exponent; i++)

why do you not write this?
function power(base, exponent) {
var result = 1;
for (var i = 0; i < exponent; i++)
result = result * base;
return result;
}
or there is also e javascript-integrated way
Math.pow(base, exponent);

when you call a function with power(2,10) you give variable "base" value of 2, and then it multiplies the result (which is 1) by 2 for 10 times, I see no use of that second variable "exponent"

Related

Javescript create list of numbers 0 to n [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 12 months ago.
Improve this question
Sorry, I dont know anything about programming, so bare with me.
I need an expression for a textlayer in adobe after effects which uses javascript. The expression needs to create a customizable list of numbers using 3 variables -
example:
x = 0 //starting point (the first number in the list)
y= 20 //increments (the size of the steps)
z= 5 //the number of steps
the output needs to be a list with each entry in a new line, in this case:
0
20
40
60
80
Hope somebody can help. Thanks
x = 0 //starting point (the first number in the list)
y= 20 //increments (the size of the steps)
z= 5 //the number of steps
let output=[]
for(let i=x;i<z;i++){
console.log(i*y)
output.push(i*y)
}
console.log(output)
let arr = [];
let x = 0;
let y = 20;
let z = 5;
for (let i = 0; i < z * y; i+= y) {
arr.push(i);
}
console.log(arr);

Make a recursive function in JavaScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to make a recursive function for this parameters. Function should determine the nth element of the row
a_0 = 1
a_k = k*a_(k-1) + 1/k
k = 1,2,3...
I am trying really hard to do this but i cant get a result. Please help me to do this
I came up with this but it is not a recursive function. I can not do this as a recursive function
let a = 1
let k = 1
let n = 3
for (let i = 1; i<=n; i++){
a = i*a + 1/i
}
console.log(a)
Here's the recursive function you're looking for, it has two conditions:
k == 0 => 1
k != 0 => k * fn(k - 1) + 1/k
function fn(k) {
if(k <= 0) return 1;
return k * fn(k - 1) + 1/k;
}
console.log(fn(1));
console.log(fn(2));
console.log(fn(3));
console.log(fn(4));
Note: I changed the condition of k == 0 to k <= 0 in the actual function so it won't stuck in an infinite loop if you pass a negative k

Calculating trailing zero in javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How do i calculate the number of trailing zeros in a factorial of a given number.
N! = 1 * 2 * 3 * 4 ... N
Any Help on this?
Because zeros come from factors 5 and 2 being multiplied together, iterate over all numbers from 1 to the input number, adding to a cumulative count of fives and twos whenever those factors are found. Then, return the smaller of those two counts:
function zeroCount(n) {
let fives = 0;
let twos = 0;
for (let counter = 2; counter <= n; counter++) {
let n = counter;
while (n % 2 === 0) {
n /= 2;
twos++;
}
while (n % 5 === 0) {
n /= 5;
fives++;
}
}
return Math.min(fives, twos);
}
console.log(zeroCount(6)); // 720
console.log(zeroCount(10)); // 3628800
It is very simple, This will help you.
function TrailingZero(n)
{
var c = 0;
for (var i = 5; n / i >= 1; i *= 5)
c += parseInt(n / i);
return c;
}
Let me know if you need help to understand this function.

Factorial function calling itself [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I came across this function which works fine for the purpose but I can't get why factorial can be called in a factorial() function. Can someone explain to me.
function factorial(n) {
if(n ==0) {
return 1;
}else{
return factorial(n-1) *n;
}
}
console.log(factorial(8));
//Logs 40320
It's called recursion. What's recursion?
Recursion is simply when a function calls itself.
You can find out more of it in JavaScript here
This is a Software Engineering (Data Structure and Algorithms) concept called recursion.
Recursion is the programming concept where a function calls itself. In the case of the factorial function, from observation, for all integers n greater than 1, factorial(n) = n * factorial(n-1). E. g. factorial(5) = 5 * factorial(4)
this means, in the implementation, the function can call itself with (n-1) and multiply the result by n.
You can read more from here.
Recursion has advantages and disadvantages.
please read this code it's work.
function factorialize(num) {
// Step 1. Create a variable result to store num
var result = num;
// If num = 0 OR num = 1, the factorial will return 1
if (num === 0 || num === 1)
return 1;
// Step 2. Create the WHILE loop
while (num > 1) {
num--; // decrementation by 1 at each iteration
result = result * num; // or result *= num;
/*
num num-- var result result *= num
1st iteration: 5 4 5 20 = 5 * 4
2nd iteration: 4 3 20 60 = 20 * 3
3rd iteration: 3 2 60 120 = 60 * 2
4th iteration: 2 1 120 120 = 120 * 1
5th iteration: 1 0 120
End of the WHILE loop
*/
}
// Step 3. Return the factorial of the provided integer
return result; // 120
}
console.log(factorialize(5));

javascript program without using math. pow, find power of any number using for loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I need to know the code built in for the syntax math.pow(x,y). Actually I used the syntax to find exponent of any number... e.g.
var e = Math.pow(-3, 3); yields -27 but couldn't find out the program behind this... Help me please
If you know what power means..
multiplying the number x n times where x is base and n is exponent.
So you just have to repeat the same thing over and over - and that's why loops are for:
var sum = 1; //note that it's not zero!
for (int i=0;i<n;i++) { //loops n times
sum = sum * x; //on each loop multiplies sum by base number
}
Did you mean alternative for Math.pow? Here is one way with simple loop.
function pow(base,power) {
var p = 1;
for (var i=0; i<power; i++) {
p *= base;
}
return p;
}
You can also use recursion to solve this kind of challenge. Beware that recursion has the disadvantage of increasing space complexity as compared to a for-loop.
function pow(base, power) {
if (power === 1) return base * power
return base * pow(base, power - 1)
}
This is a better way to calculate power of a number with recursion:
function power(base, exp) {
if(exp === 0){
return 1;
}
return base * power(base, exp - 1);
}
You can try this:
function pow(n, e) {
let num = n;
for (let i = 1; i < e; i++) {
num *= n;
}
return num;
}
console.log(pow(-3, 3));
It will give you the required result.

Categories