用Chatgpt來改錯|JavaScript-Fibonacci
IPFS
Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The next number is found by adding up the two numbers before it:
- the 2 is found by adding the two numbers before it (1+1),
- the 3 is found by adding the two numbers before it (1+2),
- the 5 is (2+3), and so on!
- first two numbers must be 0 and 1
solution checker is expecting an array as the correct output.
錯題記錄:
function fibonacciGenerator(){ for (var i = 0 /*should be 2*/; i < 100; i++){ var output = [0 , 1]; var newOutput = output.push([[i-1]+output[i-2]]); //The push() method returns the new length of the output array, not the updated array itself. //This will not work as expected, and will instead create a nested array containing the sum of i-1 and output[i-2] return newOutput; } } console.log(fibonacciGenerator())
另一個錯題:
我問:What's wrong with my code?
I wonder know why would I get the answer [0,1,1] if I write code like this:
function fibonacciGenerator(){ var output = [0 , 1]; for (var i = 2; i < 100; i++){ var newNumber = output[i-1] + output[i-2]; output.push(newNumber); return output; } } console.log(fibonacciGenerator());
回答
//The reason why you're getting the output [0, 1, 1] with this code is because the return statement is inside the for loop, //which means that the function will return the output array after the first iteration of the loop. //On the first iteration of the loop, the newNumber variable is calculated correctly, //and the new number is pushed to the output array using the push() method. //However, immediately after that, the return statement is executed, which returns the output array containing only the first two numbers [0, 1, 1]. //correct version: function fibonacciGenerator(){ var output = [0, 1]; // Initialize the output array with the first two numbers for (var i = 2; i < 100; i++){ // Loop from the third number up to 100 var newNumber = output[i-1] + output[i-2]; // Calculate the next number in the sequence output.push(newNumber); // Add the new number to the output array } return output; // Return the full sequence } console.log(fibonacciGenerator());
**return statement位置錯誤或忘了寫是我常犯的毛病
錯題
function fibonacciGenerator(newItem){ var output = [0,1]; var newItemNum = output[newItem-1] + output[newItem-2]; output.push(newItemNum); return output; } console.log(fibonacciGenerator(5)); //For example, if newItem is 5, then the function should generate the 5th number in the sequence, //which is 3 (since the sequence starts with 0, 1, 1, 2, 3). //However, if you calculate newItemNum based on output[newItem-1] and output[newItem-2], you will get output[4] and output[3], //which are both undefined since the output array has only two elements at this point.
正確答案:
function fibonacciGenerator(newItem){ var output = [0, 1]; // Initialize the output array with the first two numbers for (var i = 2; i < newItem; i++){ // Loop from the third number up to the newItem-th number var newNumber = output[i-1] + output[i-2]; // Calculate the next number in the sequence output.push(newNumber); // Add the new number to the output array } return output; // Return the full sequence } console.log(fibonacciGenerator(1)); // Output the sequence up to the third number
其他答案:
function fibonacciGenerator(n){ var output =[]; if (n === 1){ output = [0]; } else if (n === 2){ output = [0,1]; } else { output = [0,1]; for (var i=2; i<n; i++){ output.push(output[i-2] + output[i -1]); } } return output; } console.log(fibonacciGenerator(10));
問題自Web development course by Angela Yu
喜欢我的作品吗?别忘了给予支持与赞赏,让我知道在创作的路上有你陪伴,一起延续这份热忱!