用Chatgpt来改错|JavaScript-FizzBuzz
2023-04-28分类:人工智能 阅读()
自学编程很难持续的一个问题是遇到了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());
Tags: ChatGPT
标签云
-
CoinMarketCap 炒币 币圈 Rust MOVE IFO filecoin GRT near AAVE DAI Ethereum TVL 加密钱包 ERC20 区块链应用 零知识证明 区块链公司 什么是DeFi BOBA 区块链游戏 DePIN 比特币是什么 加密货币钱包 加密货币 FIL 比特币ETF 比特币挖矿 比特币减半 虚拟货币 比特币交易 加密货币投资 比特币投资 Coinw 数字货币交易所 区块链交易所 区块链开发 矿机 BitMEX OKCoin 比特币钱包 狗狗币怎么买 以太币 虚拟货币交易所 加密货币诈骗 中本聪 加密货币挖矿 BitoPro 什么是区块链 SHIB