ChatGPT-3.5 程式碼除錯
使用 ChatGPT-3.5 除錯程式碼
使用 ChatGPT-3.5 編寫程式碼就像有一位經驗豐富的程式設計師在檢查您的程式碼。
很難發現自己程式碼中的錯誤,而發現別人程式碼中的錯誤則更難。
ChatGPT 可以為您節省大量除錯程式碼的時間。
縮小問題範圍
在使用生成式 AI 幫助您之前,請嘗試縮小問題範圍並收集更多資訊。
弄清楚(如果可能)
- 程式碼的哪個部分導致了錯誤?
- 有任何錯誤訊息嗎?
- 發生了什麼,應該發生什麼?
ChatGPT 可以提供更多資訊,更準確地找到問題。
問題程式碼
上一章,我們讓 ChatGPT 為我們編寫了一些網頁程式碼。現在我們為頁面添加了一個新設計,但程式碼不再有效。
示例
<!DOCTYPE html>
<html>
<head>
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://w3schools.tw/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<style>
body,h1 {font-family: "Raleway", sans-serif}
body, html {height: 100%}
.bgimg {
background-image: url('/w3images/forestbridge.jpg');
min-height: 100%;
background-position: center;
background-size: cover;
}
</style>
</head>
<body>
<div class="bgimg w3-display-container w3-animate-opacity w3-text-white">
<div class="w3-display-middle">
<h1 class="w3-jumbo w3-animate-top">WEEKEND</h1>
<hr class="w3-border-grey" style="margin:auto;width:40%">
<p class="w3-large w3-center">35 days left</p>
</div>
<div class="w3-display-bottomleft w3-padding-large">
Powered by <a href="https://w3schools.tw/w3css/default.asp" target="_blank">w3.css</a>
</div>
</div>
自己動手試一試 »
我們知道我們更改了程式碼的哪個部分,如果我們按下 F12(或進入瀏覽器的開發者模式),我們可以在“Console”中看到錯誤。
Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')
countdown @ unknown
window.onload @ unknown
有了這些資訊,我們來讓 ChatGPT 進行除錯。
示例
使用以下提示
以下頁面的 countdown 函式不再工作。
<!DOCTYPE html>
<html>
<head>
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://w3schools.tw/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<style>
body,h1 {font-family: "Raleway", sans-serif}
body, html {height: 100%}
.bgimg {
background-image: url('/w3images/forestbridge.jpg');
min-height: 100%;
background-position: center;
background-size: cover;
}
</style>
</head>
<body>
<div class="bgimg w3-display-container w3-animate-opacity w3-text-white">
<div class="w3-display-middle">
<h1 class="w3-jumbo w3-animate-top">WEEKEND</h1>
<hr class="w3-border-grey" style="margin:auto;width:40%">
<p class="w3-large w3-center">35 days left</p>
</div>
<div class="w3-display-bottomleft w3-padding-large">
Powered by <a href="https://w3schools.tw/w3css/default.asp" target="_blank">w3.css</a>
</div>
</div>
<script>
// Function to calculate the countdown
function countdown() {
// Get the current date
var currentDate = new Date();
// Check if it's Saturday
if (currentDate.getDay() === 6) {
var countdownElement = document.getElementById("weekend_countdown");
countdownElement.innerHTML = "The Weekend has landed";
return; // Exit the function early if it's Saturday
}
// Find the next Saturday
var daysToSaturday = (6 - currentDate.getDay() + 7) % 7; // Number of days until the next Saturday
var nextSaturday = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate() + daysToSaturday);
// Calculate the remaining time
var timeRemaining = nextSaturday.getTime() - currentDate.getTime();
var daysRemaining = Math.floor(timeRemaining / (1000 * 60 * 60 * 24));
var hoursRemaining = Math.floor((timeRemaining % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
// Display the countdown on the web page
var countdownElement = document.getElementById("weekend_countdown");
countdownElement.innerHTML = daysRemaining + " days and " + hoursRemaining + " hours";
}
// Call the countdown function when the page loads
window.onload = function() {
countdown();
};
</script>
</body>
</html>
I get the following error
Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')
countdown @ unknown
window.onload @ unknown
ChatGPT-3.5 的響應可能是

審查程式碼
檢視 ChatGPT 的響應,它似乎很有可能有效。
我們不小心刪除了應該顯示文字的元素。為新元素新增正確的 ID 應該可以解決問題。
測試
將修復新增到程式碼中並進行測試
示例
<div class="w3-display-middle">
<h1 class="w3-jumbo w3-animate-top">WEEKEND</h1>
<hr class="w3-border-grey" style="margin:auto;width:40%">
<p class="w3-large w3-center" id="weekend_countdown">35 days left</p>
</div>
自己動手試一試 »
有效!