iT邦幫忙

2025 iThome 鐵人賽

DAY 4
0
Modern Web

Modern Web × AI《拖延怪日記》:語錄陪伴擺脫拖延系列 第 4

【Day 4】- 入門 JavaScript 網頁架設 :用 MVP 讓 Gemini CLI 幫忙寫第一段程式碼

  • 分享至 

  • xImage
  •  

摘要
介紹最小可行產品 (MVP) 如何在 coding 裡應用,啟發初學者構思出一個簡單的 code 架構,以降低新手對新領域的心理負擔。接著讓 Gemini CLI 根據 MVP 架構撰寫能跑的程式碼,與如何利用 CLI 進行即時 debug。

什麼是最小可行產品(minimum viable product, MVP)?

在 coding 裡,MVP 代表一個最小又能運作的程式。為什麼先做一個最小功能?因為這能幫助我們確認程式的邏輯基礎與開發方向,避免浪費時間在無止境的 debug。
簡單來說,與其一次構思完所有功能,不如先做出能跑的,再慢慢優化!

但 MVP 不只是「最小骨架程式」,它還有更重要的價值:

  • 快速驗證想法:先用小功能測試需求,確認方向正確。
  • 降低開發風險:避免投入大量資源在沒人需要的功能。
  • 收集使用者行為:觀察使用者怎麼用、收集數據,後續改進才有依據。

例如這篇等等會提到的「拖延表單 + 語錄」MVP,不只能顯示鼓勵語錄,還能延伸成紀錄最常見的拖延原因,或追蹤回訪次數,讓開發從「能跑」進化成「能驗證」與「能成長」。

Gemini CLI 幫忙擺脫「空白頁恐懼」

有想做出的功能卻不知道怎麼下手嗎?將你的想法給 AI 幫你整理點子並編排程式架構吧!
這裡提供一個 MVP 範例給你參考:

  1. 引起拖延人的共鳴畫面
  2. 拖延記錄表單 + 下拉原因 + 顯示語錄
  3. 儲存語錄到 localStorage
  4. 語錄回饋畫面
  5. 顯示紀錄(歷史語錄回顧)

不清楚怎麼開 Gemini CLI?這裡給你快速開啟步驟:

當你有一個 MVP 架構,AI 可以幫你照這個架構撰寫基礎程式。今天先以表單+語錄(無localStorage)舉例:
表單+原因下拉 → 顯示對應語錄
產生一個 index.html:一個輸入想做的事的 input、一個原因下拉選單(太累、沒心情、太多干擾、太難、不知道怎麼開始、其他),按送出後在頁面顯示一則對應原因的鼓勵語錄。

#CLI 生成的 index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Procrastinator MVP</title>
</head>
<body>
    <h1>I want to...</h1>
    <input type="text" id="taskInput" placeholder="Enter your task...">
    <select id="reasonSelect">
        <option value="tired">太累 (Tired)</option>
        <option value="mood">沒心情 (Not in the mood)</option>
        <option value="distractions">太多干擾 (Too many distractions)</option>
        <option value="difficult">太難 (Too difficult)</option>
        <option value="dontKnow">不知道怎麼開始 (Don't know how to start)</option>
        <option value="other">其他 (Other)</option>
    </select>
    <button onclick="showQuote()">Submit</button>
    <p id="quote"></p>

    <script>
        function showQuote() {
            const reason = document.getElementById('reasonSelect').value;
            const quotes = {
                tired: "休息不是懶惰,夏天躺在草地上,聽著潺潺流水,看著白雲飄過,絕非浪費時間。 (Rest is not idleness, and to lie sometimes on the grass under trees on a summer's day, listening to the murmur of the water, or watching the clouds float across the sky, is by no means a waste of time.)",
                mood: "太陽每天都在提醒我們,我們也可以從黑暗中再次升起,我們也可以照亮自己的光芒。 (The sun is a daily reminder that we too can rise again from the darkness, that we too can shine our own light.)",
                distractions: "成功的戰士是普通人,但有著雷射般的專注力。 (The successful warrior is the average man, with laser-like focus.)",
                difficult: "河流之所以能切穿岩石,不是因為它的力量,而是因為它的堅持。 (A river cuts through rock, not because of its power, but because of its persistence.)",
                dontKnow: "成功的秘訣在於開始。 (The secret of getting ahead is getting started.)",
                other: "相信自己能做到,你就已經成功了一半。 (Believe you can and you're halfway there.)"
            };
            document.getElementById('quote').innerText = quotes[reason];
        }
    </script>
</body>
</html>

我該怎麼把它跑起來?

想做的事、原因下拉選單:
https://ithelp.ithome.com.tw/upload/images/20250819/20177913g0lFACjX1j.png

送出後顯示對應原因的鼓勵語錄:
https://ithelp.ithome.com.tw/upload/images/20250819/201779134fhDGLi9WJ.png

Gemini CLI 的 Debug 流程

當系統出現一堆看不懂的紅字時,不要慌張,直接貼給AI,讓他解釋出現什麼問題、怎麼解決,也可以訊問該問題相關的背景知識。
舉例(淺灰字是貼上的報錯紅字內容):
https://ithelp.ithome.com.tw/upload/images/20250819/20177913vpdZLQPpUA.png

除錯流程進階版

  1. 寫下「期望 vs 程式碼實際行為」。
  2. 附上相關系統的版本,例如:Node、npm、作業系統與 CLI 版本。
  3. 貼上錯誤訊息與相關檔案片段/專案樹。

是不是很簡單?後續我們會以這個 MVP 架構為基礎一步步架網頁!


上一篇
【Day 3】- VS Code 初學者適合 Gemini CLI 還是 Github Copilot ( GPT-4.1 )?
下一篇
【Day 5】- 入門 JavaScript 網頁架設:LocalStorage
系列文
Modern Web × AI《拖延怪日記》:語錄陪伴擺脫拖延19
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言