Cuba fahami jawapan kepada coretan kod daripada buku Eloquent JavaScript
P粉986937457
P粉986937457 2024-04-02 15:03:32
0
1
309

Ini ialah coretan kod daripada Bab 3 Eloquent JavaScript.

const power = function(base, exponent) {
  let result = 1;
  for (let count = 0; count < exponent; count++) {
    result *= base;
  }
  return result;
};
console.log(power(2, 10));
// 1024

Beginilah saya menerangkannya kepada diri saya sendiri. Tetapi saya tidak faham mengapa 1024 someene dikembalikan. Bolehkah anda membantu saya memecahkannya?

/* 
    Explanation of code above.

     - We created a function named power. We used a function expression notation to create it. 
     
     - power has two parameters base and exponent.

     - the body of the function contains the code that the function will execute. 

    - the body does the following:

        1. declares a variable called result which has a value of 1.
        
        2. There is a for loop. Here are the parts of the for loop:

            - The intializer: the variable count is declared and assigned a value of 0

            - We are looking at the condition to see if it is truthy. The condition is whether count is greater than exponent. The loop will continue to iterate until this condition is truthy.

            - The incrementer: count will go up by 1 each iteration of the loop

            - The body of the for loop (the code to be executed) states that the result variable is equal to result *= base OR result = result * base 
    
    - then the function will return the value of result 

    - we are done with the function 

    - outside of the function we invoke it using console.log() and pass it the arguments 2 and 10 (base, exponent). 

    - The computer plugs the arguments into the function. The computer evaluates the condition in the for loop to see if its truthy. Basically, the computer asks is count < exponent? Count = 0 and exponent = 10. So the condition evaluates to falsey. 


    const power = function (2, 10) {
    let result = 1;
    for (let count = 0; 11 < 10; count++) {
                    1   10
                    2   10
                    3   10
                    4   10
                    5   10
                    6   10
                    7   10
                    8   10
                    9   10
                    10  10
                    11  10
                    
        result *= base; => 2 = 2 * 10
    }
    return result;

};

console.log(power(2,10));


*/

Saya menjangkakan hasil * = asas, atau hasil = hasil * asas. Saya tahu saya kehilangan sesuatu dalam lelaran.

P粉986937457
P粉986937457

membalas semua(1)
P粉005417748
for(let count = 0; count 

Nampaknya anda sudah faham akibat kesilapan ini. Ambil perhatian bahawa nilai count 变量仅用于控制循环的迭代次数。每次迭代时,您都会更新 result 变量以包含新值,方法是将其与 base didarab dan kemudian digunakan untuk pengiraan seterusnya. Jadi ia akan menjadi seperti ini.

1 * 2 = 2
2 * 2 = 4
4 * 2 = 8
8 * 2 = 16
16 * 2 = 32
32 * 2 = 64
64 * 2 = 128
128 * 2 = 256
256 * 2 = 512
512 * 2 = 1024
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!