使用 ChatGPT-4 編寫代碼就像有一個經(jīng)驗(yàn)豐富的程序員幫助你。
如果您知道如何提問,ChatGPT 可以為您節(jié)省大量編碼時間!
在使用生成式 AI 來幫助你之前,請為你的代碼設(shè)定一個明確的目標(biāo)。
目標(biāo)示例:
一般來說,在使用生成式 AI 時,清晰度和上下文很重要,但在使用它們編寫代碼時,它更為重要!
例如,寫“創(chuàng)建一個函數(shù),用于倒計(jì)時到下一個星期六的天數(shù)和小時數(shù)”,而不是“編寫代碼以查找最近的星期六”
要更具體地說,請指定您需要的編程語言。
如果您不確定使用哪種編程語言,可以詢問 ChatGPT:
使用以下提示:
Which programming language should I use to create a countdown to the next Saturday for a web page?
ChatGPT-4 的回應(yīng)可能是:
現(xiàn)在我們可以進(jìn)一步闡明我們的任務(wù):“創(chuàng)建一個 JavaScript 函數(shù),用于倒計(jì)時到下一個星期六的天數(shù)和小時數(shù)。
現(xiàn)在,我們可以使用 ChatGPT 來編寫代碼。
假設(shè)您有以下網(wǎng)頁:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>The weekend is only <span id="weekend_coundown"></span>from now!</p>
</body>
</html>
讓 ChatGPT 編寫代碼:
使用以下提示:
For the following web page:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>The weekend is only <span id="weekend_coundown"></span>from now!</p>
</body>
</html>
Create a JavaScript function that counts down the number of days and hours until the next Saturday.
ChatGPT-4 的回應(yīng)可能是:
然后測試它:
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>The weekend is only <span id="weekend_coundown"></span> from now!</p>
<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 0.
if (daysToSaturday === 0 && currentHour >= 12) {
days = "";
hours = "0 hours";
}
document.getElementById('weekend_coundown').innerText = days + hours;
}
countdownToSaturday();
setInterval(countdownToSaturday, 1000 * 60 * 60); // update the countdown every hour
</script>
</body>
</html>
它有效!
您可以繼續(xù)使用 ChatGPT 來改進(jìn)您的代碼:
使用以下提示:
Add code to display "The Weekend has landed" instead of the countdown if the day is Saturday
ChatGPT-4 的回應(yīng)可能是:
更多建議: