解題學JavaScript #6 火箭倒數升空


Posted by yunhu412 on 2023-06-11

撰寫這一系列文章的目的,是為了紀錄我在做Udacity JavaScript線上課程的測驗時,是如何思考、如何提問,又是如何一步步修改,產出可以運作、沒有bug的程式。還有,當自己寫出一段可以順利運作但和官方解答不一樣的程式碼時,我是如何嘗試去推敲、去理解官方解答背後的撰寫邏輯是什麼。

所以,不意外地,你一定會看到我在一道題裡不停地試錯,然後把這些試錯經驗寫下來,並期待它能逐漸內化成日後寫出沒有bug的程式碼的養分。

今天這一篇比較特別,因為我只試錯了一次就成功!這個情況可能不滿足於我原先對於試錯文章的設定,畢竟改了一次就成功可以撰寫的篇幅也不多。

但我突然有股想把這次經驗記錄下來的衝動,因為這次經驗對我來說是努力投入時間、心力和精力的綜合正向回饋,也可以把它視作一種對於我駕馭JavaScript能力提升的肯定。所以,才有了這篇文章的誕生!

好,話不多說,我們進入正題吧!

(題目)

看完題目後,我寫了下面的程式碼:

var x = 60;
while(x => 0) {
    if(x===50) {
        console.log("Orbiter transfers from ground to internal power");
    } 
    else if(x===31) { 
        console.log("Ground launch sequencer is go for auto sequence start");
    }
    else if(x===16) { 
        console.log("Activate launch pad sound suppression system");
    }    
    else if(x===10) { 
        console.log("Activate main engine hydrogen burnoff system");
    }    
    else if(x===6) { 
        console.log("Main engine start");
    }
    else if(x===0) { 
        console.log("Solid rocket booster ignition and liftoff!"); 
    }
    else {
        console.log("T-"+ x + " seconds");
    }
    x = x-1;
}

就當我沉浸於「這次怎麼寫得那麼順」的喜悅,按下Test Run鍵之後......

得到一堆亂碼again!!

昨天和友人兼軟體工程師前輩C聊過後,發現這個我眼中所謂的亂碼,其實是很重要的訊息!它能告訴你程式碼哪裡出了問題,以及出了什麼樣的問題!所以看懂這些錯誤訊息,對寫程式的人來說是一件重要的事情!

循著這個方向,我今天用不同的方式問 Udacity GPT。

I:What does it means when I pressed "Test Run" bottom with code below and got feedback like this?


(上面的程式碼)


(錯誤訊息)
Traceback (most recent call last): File "vm_main.py", line 31, in import main File "/tmp/workspaces/e9355d91-a98a-4362-8d34-e3e1b3fe4f81/main.py", line 2, in import studentMain File "/tmp/workspaces/e9355d91-a98a-4362-8d34-e3e1b3fe4f81/studentMain.py", line 3, in util.run_program(['python', 'falcon.py', '-s', 'js_mocha', '-m', 'test']) File "/tmp/workspaces/e9355d91-a98a-4362-8d34-e3e1b3fe4f81/falcon/util.py", line 23, in run_program out, err = program.communicate() File "/usr/lib/python2.7/subprocess.py", line 480, in communicate self.wait() File "/usr/lib/python2.7/subprocess.py", line 1099, in wait pid, sts = _eintr_retry_call(os.waitpid, self.pid, 0) File "/usr/lib/python2.7/subprocess.py", line 125, in _eintr_retry_call return func(*args) KeyboardInterrupt

G:It looks like there was an error in your code. The error message you received is called a "traceback" and it is telling you that there was an error on line 1 of your code. Specifically, the error is caused by the => operator in your while loop condition. This operator is not valid in JavaScript. Instead, you should use the < operator to check if x is greater than 0.

GPT解釋道,我收到的「錯誤訊息」叫做"traceback",它顯示我在第一行程式碼中有個錯誤。這個錯誤是,我把">=" 寫成 "=>" 了!(在前面的題目好像也發生過一遍,只能說這個符號真的還沒內建在我的腦袋裡@@)

根據GTP的回饋,我把第一行的"=>" 改成 ">=",Test Run成功,送出答案也通過了!對照官方解答後也發現兩個寫法是一樣的!

過關的程式碼如下:

var x = 60;
while(x >= 0) {
    if(x===50) {
        console.log("Orbiter transfers from ground to internal power");
    } 
    else if(x===31) { 
        console.log("Ground launch sequencer is go for auto sequence start");
    }
    else if(x===16) { 
        console.log("Activate launch pad sound suppression system");
    }    
    else if(x===10) { 
        console.log("Activate main engine hydrogen burnoff system");
    }    
    else if(x===6) { 
        console.log("Main engine start");
    }
    else if(x===0) { 
        console.log("Solid rocket booster ignition and liftoff!"); 
    }
    else {
        console.log("T-"+ x + " seconds");
    }
    x = x-1;
}

值得慶祝的「修改一次」就過關!


#javascript #Coding #code #Udacity #程式 #前端







Related Posts

[React] 一些 Hooks 的分類

[React] 一些 Hooks 的分類

[24] 強制轉型 - parseInt、Number、ToPrimitive、Boolean

[24] 強制轉型 - parseInt、Number、ToPrimitive、Boolean

 Day 171

Day 171


Comments