[ Web Proxy ]
URL:
Viewing: https://learn.javascript.ru/recursion [Back]  [Original]

:
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek
6 2025 .

.

.

, , , .

, , , . . , , .

. . .

pow(x, n), x n. , x n .

pow(2, 2) = 4
pow(2, 3) = 8
pow(2, 4) = 16

.

  1. : for:

    function pow(x, n) {
      let result = 1;
    
      //  result  x n   
      for (let i = 0; i < n; i++) {
        result *= x;
      }
    
      return result;
    }
    
    alert( pow(2, 3) ); // 8
  2. : :

    function pow(x, n) {
      if (n == 1) {
        return x;
      } else {
        return x * pow(x, n - 1);
      }
    }
    
    alert( pow(2, 3) ); // 8

, .

pow(x, n) , :

              if n==1  = x
             /
pow(x, n) =
             \
              else     = x * pow(x, n - 1)
  1. n == 1, . , : pow(x, 1) x.
  2. pow(x, n) : x * pow(x, n - 1). : xn = x * xn-1. : ( x) (pow n). , n 1.

, pow n == 1.

[]

, pow(2, 4) :

  1. pow(2, 4) = 2 * pow(2, 3)
  2. pow(2, 3) = 2 * pow(2, 2)
  3. pow(2, 2) = 2 * pow(2, 1)
  4. pow(2, 1) = 2

, , , , .

, .

? if, pow(x, n), , :

function pow(x, n) {
  return (n == 1) ? x : (x * pow(x, n - 1));
}

( ) . n.

JavaScript. 10000 , , 100000 . , ( ), .

, : , .

,

, . .

(execution context).

, . , , , this ( ) .

, .

, :

  • .
  • , , .
  • , .
  • , , .

pow(2, 3).

pow(2, 3)

pow(2, 3) : x = 2, n = 3, .

:

  • : { x: 2, n: 3, 1 } pow(2, 3)

. n == 1 , if:

function pow(x, n) {
  if (n == 1) {
    return x;
  } else {
    return x * pow(x, n - 1);
  }
}

alert( pow(2, 3) );

, , :

  • : { x: 2, n: 3, 5 } pow(2, 3)

x * pow(x, n - 1), pow pow(2, 2).

pow(2, 2)

JavaScript .

pow, . :

  1. .
  2. .
  3. , .

pow(2, 2):

  • : { x: 2, n: 2, 1 } pow(2, 2)
  • : { x: 2, n: 3, 5 } pow(2, 3)

( ), .

, , , , . , .

pow(2, 1)

: 5, x=2, n=1.

, :

  • : { x: 2, n: 1, 1 } pow(2, 1)
  • : { x: 2, n: 2, 5 } pow(2, 2)
  • : { x: 2, n: 3, 5 } pow(2, 3)

pow(2, 1).

pow(2, 1), , n == 1 , if:

function pow(x, n) {
  if (n == 1) {
    return x;
  } else {
    return x * pow(x, n - 1);
  }
}

, , 2.

, , , :

  • : { x: 2, n: 2, 5 } pow(2, 2)
  • : { x: 2, n: 3, 5 } pow(2, 3)

pow(2, 2). pow(2, 1), x * pow(x, n - 1), 4.

:

  • : { x: 2, n: 3, 5 } pow(2, 3)

, : pow(2, 3) = 8.

3.

, , .

. , , n n .

:

function pow(x, n) {
  let result = 1;

  for (let i = 0; i < n; i++) {
    result *= x;
  }

  return result;
}

pow , i result. , n.

. , .

, , , . .

, . , , , .

.

, . :

let company = {
  sales: [{
    name: 'John',
    salary: 1000
  }, {
    name: 'Alice',
    salary: 600
  }],

  development: {
    sites: [{
      name: 'Peter',
      salary: 2000
    }, {
      name: 'Alex',
      salary: 1800
    }],

    internals: [{
      name: 'Jack',
      salary: 1300
    }]
  }
};

, .

  • . , sales 2 : .

  • , , development : sites internals. .

  • , ( ).

    , sites siteA siteB. . , .

, , . ?

, . , for company 1- . , sites 3- , ? 3-4 , .

.

, , :

  1. .
  2. N N , , .

(1), , , .

(2), , . . , , , , (1).

:

let company = { //    ,   
  sales: [{name: 'John', salary: 1000}, {name: 'Alice', salary: 600 }],
  development: {
    sites: [{name: 'Peter', salary: 2000}, {name: 'Alex', salary: 1800 }],
    internals: [{name: 'Jack', salary: 1300}]
  }
};

//     
function sumSalaries(department) {
  if (Array.isArray(department)) { //  (1)
    return department.reduce((prev, current) => prev + current.salary, 0); //   
  } else { //  (2)
    let sum = 0;
    for (let subdep of Object.values(department)) {
      sum += sumSalaries(subdep); //    ,  
    }
    return sum;
  }
}

alert(sumSalaries(company)); // 6700

(?). . .

:

[]

: {...} , [...] , .

, , :

  • arr.reduce .
  • for(val of Object.values(obj)) : Object.values .

( ) , .

.

:

  • .
  • .

- : HTML- XML-.

HTML- HTML- :

  • .
  • HTML-.
  • HTML- (, , / ..).

.

, .

, .

:

let arr = [obj1, obj2, obj3];

. . , arr.unshift(obj) , obj, , , . arr.shift().

, , : arr.push/pop. , , .

, /, , .

:

  • value,
  • next , null, .

:

let list = {
  value: 1,
  next: {
    value: 2,
    next: {
      value: 3,
      next: {
        value: 4,
        next: null
      }
    }
  }
};

:

[]

:

let list = { value: 1 };
list.next = { value: 2 };
list.next.next = { value: 3 };
list.next.next.next = { value: 4 };
list.next.next.next.next = null;

, , value next, . list , , next , .

:

let secondList = list.next.next;
list.next.next = null;
[]

:

list.next.next = secondList;

, , .

, :

let list = { value: 1 };
list.next = { value: 2 };
list.next.next = { value: 3 };
list.next.next.next = { value: 4 };
list.next.next.next.next = null;

//     
list = { value: "new item", next: list };
[]

, next :

list.next = list.next.next;
[]

list.next 1 2. 1 . - , .

, , .

, . .

, . : arr[n] . next N , N- .

. , , / , .

:

  • prev next , .
  • tail, ( / ).
  • : , .

:

  • , . .

    , . , , .

  • , .

    , , , ( null).

    list = { value, next -> list }

    , HTML- , : , .

    sumSalary, .

. . .

: 5

sumTo(n), 1 + 2 + ... + n.

:

sumTo(1) = 1
sumTo(2) = 2 + 1 = 3
sumTo(3) = 3 + 2 + 1 = 6
sumTo(4) = 4 + 3 + 2 + 1 = 10
...
sumTo(100) = 100 + 99 + ... + 2 + 1 = 5050

:

  1. .
  2. , .. sumTo(n) = n + sumTo(n-1) for n > 1.
  3. .

:

function sumTo(n) { /*...   ... */ }

alert( sumTo(100) ); // 5050

P.S. ? ? ?

P.P.S. sumTo(100000)?

:

function sumTo(n) {
  let sum = 0;
  for (let i = 1; i <= n; i++) {
    sum += i;
  }
  return sum;
}

alert( sumTo(100) );

:

function sumTo(n) {
  if (n == 1) return 1;
  return n + sumTo(n - 1);
}

alert( sumTo(100) );

: sumTo(n) = n*(n+1)/2:

function sumTo(n) {
  return n * (n + 1) / 2;
}

alert( sumTo(100) );

P.S. , ? . n, n .

. , , . .

P.P.S. : , - , . . JavaScript- ( ), : , .

: 4

, " ", " ", 1. n n!

:

n! = n * (n - 1) * (n - 2) * ...*1

n:

1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24
5! = 5 * 4 * 3 * 2 * 1 = 120

factorial(n), n!, .

alert( factorial(5) ); // 120

P.S. : n! n * (n-1)! : 3! = 3*2! = 3*2*1! = 6

n! n * (n-1)!.

, factorial(n) n factorial(n-1). n-1, , 1.

function factorial(n) {
  return (n != 1) ? n * factorial(n - 1) : 1;
}

alert( factorial(5) ); // 120

1. 0, :

function factorial(n) {
  return n ? n * factorial(n - 1) : 1;
}

alert( factorial(5) ); // 120
: 5

Fn = Fn-1 + Fn-2. , .

1, 2(1+1), 3(1+2), 5(2+3) : 1, 1, 2, 3, 5, 8, 13, 21....

.

fib(n) n- .

:

function fib(n) { /*   */ }

alert(fib(3)); // 2
alert(fib(7)); // 13
alert(fib(77)); // 5527939700884757

P.S. . fib(77) .

.

:

function fib(n) {
  return n <= 1 ? n : fib(n - 1) + fib(n - 2);
}

alert( fib(3) ); // 2
alert( fib(7) ); // 13
// fib(77); //   

n . , fib(77) , .

, . .

, fib(5):

...
fib(5) = fib(4) + fib(3)
fib(4) = fib(3) + fib(2)
...

, fib(3) fib(5) fib(4). , .

:

[]

, fib(3) , fib(2) . , n, n=77.

, : , , fib(3) , .

.

, n , , 1 2, fib(3) , fib(4) , fib(5) , . .

.

:

// a = fib(1), b = fib(2),      1
let a = 1, b = 1;

//  c = fib(3)   
let c = a + b;

/*     fib(1), fib(2), fib(3)
a  b  c
1, 1, 2
*/

fib(4) = fib(2) + fib(3).

: a,b, fib(2),fib(3), c :

a = b; //  a = fib(2)
b = c; //  b = fib(3)
c = a + b; // c = fib(4)

/*  :
   a  b  c
1, 1, 2, 3
*/

:

a = b; // now a = fib(3)
b = c; // now b = fib(4)
c = a + b; // c = fib(5)

/*   (   ):
      a  b  c
1, 1, 2, 3, 5
*/

, . .

:

function fib(n) {
  let a = 1;
  let b = 1;
  for (let i = 3; i <= n; i++) {
    let c = a + b;
    a = b;
    b = c;
  }
  return b;
}

alert( fib(3) ); // 2
alert( fib(7) ); // 13
alert( fib(77) ); // 5527939700884757

i=3, a=1, b=1.

.

: 5

, ( ):

let list = {
  value: 1,
  next: {
    value: 2,
    next: {
      value: 3,
      next: {
        value: 4,
        next: null
      }
    }
  }
};

printList(list), .

: .

: ?

:

let list = {
  value: 1,
  next: {
    value: 2,
    next: {
      value: 3,
      next: {
        value: 4,
        next: null
      }
    }
  }
};

function printList(list) {
  let tmp = list;

  while (tmp) {
    alert(tmp.value);
    tmp = tmp.next;
  }

}

printList(list);

, tmp . , list :

function printList(list) {

  while(list) {
    alert(list.value);
    list = list.next;
  }

}

. , - . list, .

, list , . , .

, tmp , i for.

printList(list) : list, list.next:

let list = {
  value: 1,
  next: {
    value: 2,
    next: {
      value: 3,
      next: {
        value: 4,
        next: null
      }
    }
  }
};

function printList(list) {

  alert(list.value); //   

  if (list.next) {
    printList(list.next); //        
  }

}

printList(list);

?

, . , .

, , , .

: 5

.

: .

.

, :

let list = {
  value: 1,
  next: {
    value: 2,
    next: {
      value: 3,
      next: {
        value: 4,
        next: null
      }
    }
  }
};

function printReverseList(list) {

  if (list.next) {
    printReverseList(list.next);
  }

  alert(list.value);
}

printReverseList(list);

, .

list. , .

, , , :

let list = {
  value: 1,
  next: {
    value: 2,
    next: {
      value: 3,
      next: {
        value: 4,
        next: null
      }
    }
  }
};

function printReverseList(list) {
  let arr = [];
  let tmp = list;

  while (tmp) {
    arr.push(tmp.value);
    tmp = tmp.next;
  }

  for (let i = arr.length - 1; i >= 0; i--) {
    alert( arr[i] );
  }
}

printReverseList(list);

, : , ( ), .


Web Proxy Viewer  |  New URL  |  Original Page