ChatGPT-4 程式碼除錯
使用 ChatGPT-4 除錯程式碼
使用 ChatGPT-4 編寫程式碼,就像有一位經驗豐富的程式設計師在審查你的程式碼一樣。
當你寫程式碼時,很難發現其中的錯誤,而在審查別人的程式碼時,則更加困難。
ChatGPT 可以為你節省大量除錯程式碼的時間。
縮小問題範圍
在使用生成式 AI 幫助你之前,請先嚐試縮小問題範圍並收集更多資訊。
找出(如果可能)
- 程式碼的哪個部分導致了錯誤?
- 是否有任何錯誤訊息?
- 發生了什麼,本應發生什麼?
ChatGPT 能夠提供更準確的問題查詢,前提是它擁有更多的資訊。
問題程式碼
上一章,我們讓 ChatGPT 為我們編寫了一些網頁程式碼。現在我們為頁面添加了新的設計,而程式碼不再起作用了。
示例
<!DOCTYPE html>
<html>
<head>
<title>W3.CSS 模板</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 天</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(或進入瀏覽器的開發者模式),我們可以在“控制檯”中看到錯誤。
Uncaught TypeError: Cannot set properties of null (setting 'innerText')
at countdownToSaturday
有了這些資訊,讓我們請 ChatGPT 來除錯。
示例
使用以下提示
以下頁面的 countdown 函式不再起作用了。
<!DOCTYPE html>
<html>
<head>
<title>W3.CSS 模板</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 天</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 countdownToSaturday() {
var now = new Date();
var dayOfWeek = now.getDay(); //0-6 where 0 is Sunday, 6 is Saturday
var daysToSaturday = (dayOfWeek < 6) ? (6 - dayOfWeek) : 0; // if it's already Saturday, no days remain
var currentHour = now.getHours();
var hoursToSaturday = 0;
// If it's not Saturday or if it's Saturday but before 12:00 (noon),
// consider the remaining hours to Saturday noon
if (daysToSaturday > 0 || (daysToSaturday === 0 && currentHour < 12)) {
hoursToSaturday = (24 - currentHour + 12) % 24;
}
var days = daysToSaturday > 0 ? daysToSaturday + " day(s), " : "";
var hours = hoursToSaturday > 0 ? hoursToSaturday + " hour(s)" : "";
// if it's Saturday and past 12:00 (noon), the countdown should return "The Weekend has landed".
if (daysToSaturday === 0 && currentHour >= 12) {
document.getElementById('weekend_coundown').innerText = "The Weekend has landed";
} else {
document.getElementById('weekend_coundown').innerText = days + hours;
}
}
countdownToSaturday();
setInterval(countdownToSaturday, 1000 * 60 * 60); // update the countdown every hour
</script>
</body>
</html>
我收到了以下錯誤
Uncaught TypeError: Cannot set properties of null (setting 'innerText')
at countdownToSaturday
ChatGPT-4 的響應可能是

審查程式碼
檢查 ChatGPT 的響應,它似乎很有可能起作用。
我們不小心移除了應該顯示文字的元素。為新元素新增正確的 ID 應該可以解決問題。
測試
將修復新增到程式碼中並進行測試。
有效!