繁安
繁安

喜歡開發腦洞,如果能讓你對人生有新的啟發,會是我最大的榮幸。 #心靈治愈 #星盤 #普通分享 #隨筆 #自學前端ing

用Chatgpt來改錯|JavaScript-FizzBuzz

自學編程很難持續的一個問題是遇到了bug根本不知道問題在哪裡,找論壇別人也不一定能給你解釋清楚,自從Chatgpt出了之後,你可以反复問它問題,不管難易,對方也不會挑剔,一直解釋到你懂得為止。

自學編程很難持續的一個問題是遇到了bug根本不知道問題在哪裡,找論壇別人也不一定能給你解釋清楚,自從Chatgpt出了之後,你可以反复問它問題,不管難易,對方也不會挑剔,一直解釋到你懂得為止。

Chatgpt是語言模型,所以問題要夠詳細回答也才會足夠詳細,編碼問題答案基本準確,但詢問需要用其他模型/計算方式來出答案的問題就算很基本也不會準確。

Fizz Buzz

Write a program that prints the numbers from 1 to 100.

But for multiples of three print "Fizz" instead of the number and for the multiples of five print"Buzz".

For numbers which are multiples of both three and five print "FizzBuzz".

Question and guideline from Angel Yu web development course

我的答案

var num = Math.floor(Math.random()*100+1);
function fizz(){
    if (num%3 ===0){
        if ((num%3 ===0) && (num%5 ===0)){
            return console.log("FizzBuzz");
        } else 
        return console.log("Fizz");
    } else 
    if (num%5 ===0){
        return console.log("Buzz");
    } else {
        return num;
    }
   //忘記加上
    return num;
}

console.log(fizz());

回覆:答案忽略了問題需要顯示的答案是從0到100而不是隨機數字,也忘了寫return statement。(實際上剛碼完Math.random()題目有點上頭,下意識寫了進去。)

Chatgpth回覆:

// Your answer is not correct because your fizz() function only checks the FizzBuzz conditions for a single random number generated by Math.random().
// You are not iterating through all the numbers from 1 to 100 and checking each number for the conditions. 
//Moreover, you are not passing any argument to the fizz() function, so the num variable inside the function is always referring to the same random number.

***not passing any argument to the fizz() function是重點

// The updated program that I provided uses a for loop to iterate through all the numbers from 1 to 100 and checks each number for the FizzBuzz conditions. That's why it prints the correct output.
for (var i = 1; i <= 100; i++) {
    if (i % 3 === 0 && i % 5 === 0) {
    console.log("FizzBuzz");
    } else if (i % 3 === 0) {
    console.log("Fizz");
    } else if (i % 5 === 0) {
    console.log("Buzz");
    } else {
    console.log(i);
    }
    }

其他版本答案:

    for (var i = 1; i <= 100; i++) {
        var output = "";
        output += i % 3 === 0 ? "Fizz" : "";
        output += i % 5 === 0 ? "Buzz" : "";
        console.log(output || i);
        }

沒太懂這個寫法,問Chatgpt以數字15為例解釋:

In this program, we use a for loop to iterate through the numbers from 1 to 100. 
For each number, we check if it's divisible by 3 and/or 5 using the modulo operator. 
If it's divisible by 3, we append the string "Fizz" to a variable called output. 
If it's divisible by 5, we append the string "Buzz" to output. If neither condition is true, output remains an empty string.
The += operator is used to append a string to output. It is equivalent to writing:
output = output + "Fizz";
If the number is not divisible by either 3 or 5, we print the number itself. If output is not an empty string, we print output.

Here's an example for the number 15:
var output = "";
output += 15 % 3 === 0 ? "Fizz" : ""; // output = "Fizz"
output += 15 % 5 === 0 ? "Buzz" : ""; // output = "FizzBuzz"
console.log(output || 15); // prints "FizzBuzz"

Since 15 is divisible by both 3 and 5, the program appends "Fizz" and "Buzz" to output, resulting in the string "FizzBuzz". The console.log() statement then prints output, which is "FizzBuzz".

原版影片的答案是用console.log來輸出每個數字的答案(以array顯示):

var output = [];
var count = 1;

function fizzBuzz(){
if (count%3 === 0 && count%5 === 0){
output.push("FizzBuzz");
} else
if (count%3 === 0){
output.push("Fizz");
} else
if (count%5 === 0){
output.push("Buzz");
} else {
output.push(count);
}
count++;
return output;
}

console.log(fizzBuzz()); //[ 1 ]
console.log(fizzBuzz()); //[ 1, 2 ]
console.log(fizzBuzz()); //[ 1, 2, 'Fizz' ]

另外的版本:

var count = 1;

function fizzBuzz(){
  var result;
  if (count%3 === 0 && count%5 === 0){
    result = "FizzBuzz";
  } else if (count%3 === 0){
    result = "Fizz";
  } else if (count%5 === 0){
    result = "Buzz";
  } else {
    result = count;
  }
  count++;
  return result;
}

console.log(fizzBuzz());
console.log(fizzBuzz());
console.log(fizzBuzz());
CC BY-NC-ND 2.0 版权声明

喜欢我的文章吗?
别忘了给点支持与赞赏,让我知道创作的路上有你陪伴。

加载中…
加载中…

发布评论