Microbit Jack
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ###### tags: `JS學徒特訓班` # Day10 函式參數設計 隔日早晨,小杰前往老先生家裡的路上,心裡越想越不對。 為什麼老闆會准特休,放他去一個素未謀面的老先生家裡去特訓。早知道就不要跟老先生打賭,說什麼只要老闆允許請假,就答應特訓的請求!浪費了僅有的特休,小杰感到懊惱又無奈,只能安慰自己走一步算一步。 過沒半晌,小杰來到了老先生家裡,外牆上的招牌上寫著「**Java Script 補習班**」,等等..「Java Script」?為什麼中間會有空白,難不成我一直搞錯「JavaScript」的真正拼法..? 進入大廳有個圓形長桌,同時擺放了許多 Windows 老舊筆電,桌面上相當雜亂,有著刻意排出金字塔的玻璃能量飲料,而且還有各種明顯過時的書籍,例如「**Java 實戰 - 王者歸來**」、「**Java 之你不可不學的一百零八式**」,裡面還夾了些讓人匪夷所思的照片,例如下張。 ![](https://i.imgur.com/8tioepp.png) 最後小杰目光看到了一幅相框,是 2 人在餐廳的合照,手上共同拿著「鴻圖大展」的匾額。左邊明顯是年輕時的老先生,右邊的男性越看越眼熟,但卻一直無法聯想起來。 老先生:「少年,你來了,那我們就開始吧!」 小杰嚇了一跳,老先生不知何時已經坐在旁邊,並已經開啟電腦準備待續。 老先生:「我看過你第五關寫的 code 了,雖然比不上我當年,但還算差強人意!」 小杰:「謝謝讚美...咦老先生為什麼你看過—」 老先生:「叫我師父!特訓已經開始了,來吧!」 ## 第一題:客戶詢價函式,來更多個客人也不怕 請看完[函式教學影片](https://hackmd.io/@YmcMgo-NSKOqgTGAjl_5tg/Sk-_oGL2U/%2F6zQ3SasRR2GR-F3FW8SYVw)後,觀看下面截圖,請改設計一個詢價用的函式,並新增三個參數,依序為客戶姓名、薯條數量,漢堡數量。 ![bobTotal = hamburgerPrice * 4 + friesPrice * 30 ](https://i.imgur.com/CmHOgT3.png) 同時來了三個客戶,請執行三次函式回報結果。 ## 第二題:好多客戶都在問小杰,他們錢包剩下多少錢,小杰表示無言 下面截圖,請改設計一個查詢客戶錢包餘額用的函式,並新增四個參數,依序為客戶姓名、客戶錢包總額、薯條數量,漢堡數量。 同時來了三個客戶,請執行三次函式回報結果。 ![markTotal = markWallet - hamburgerPrice * 1 + friesPrice * 3](https://i.imgur.com/dHRMz5v.png) ## 第三題:好多人都擁有九折優惠券 這一題,換您來設計函式與參數看看。 突然間同時來了三個客戶,請執行三次函式回報結果。 ![maryTotal = maryWallet - hamburgerPrice *10 *sale -friesPrice * 10 * sale](https://i.imgur.com/su5ZNi5.png) *** ## 解答 ```javascript= //共用 - 金額函式 var hamburgerPrice = 50; var friesPrice = 40; function price(friesNum, hambNum) { return friesNum * friesPrice + hambNum * hamburgerPrice; }; //共用 - 結果函式 function getResult(name, money) { if(money >= 0) { console.log(`${name} 買完東西後,錢包剩下 ${money} 元`); } else { //console.log(`${name} 您好,你所帶的金額不足,還差 ${-1 * money} 元`); console.log(`${name} 您好,你所帶的金額不足,還差 ${Math.abs(money)} 元`); } }; //第一題:客戶詢價函式,來更多客人也不怕 function askPrice(name, friesNum, hambNum) { total = price(friesNum, hambNum); console.log(`${name} 您好,您詢問的金額總計為 ${total} 元`); }; askPrice('Bob', 2, 2); //Bob 您好,您詢問的金額總計為 180 元 askPrice('Mark', 10, 2); //Mark 您好,您詢問的金額總計為 500 元 askPrice('Mary', 4, 7); //Mary 您好,您詢問的金額總計為 510 元 //第二題:好多客戶都在問小杰,他們錢包剩下多少錢,小杰表示無言 function generalCalculation(name, wallet, friesNum, hambNum) { total = price(friesNum, hambNum); var spread = wallet - total; getResult(name, spread); }; generalCalculation('Jack', 2000, 2, 7); //Jack 買完東西後,錢包剩下 1570 元 generalCalculation('John', 3400, 8, 2); //John 買完東西後,錢包剩下 2980 元 generalCalculation('Susan', 100, 5, 8); //Susan 您好,你所帶的金額不足,還差 500 元 //第三題:好多人都擁有九折優惠券 var hamburgerPrice = 50; var friesPrice = 40; var sale = 0.9; function discountCalculation(name, wallet, friesNum, hambNum) { total = price(friesNum, hambNum) * sale; var spread = wallet - total; getResult(name, spread); }; discountCalculation('JavaScript', 1000, 10, 10); //JavaScript 買完東西後,錢包剩下 190 元 discountCalculation('CSharp', 1200, 20, 30); //CSharp 您好,你所帶的金額不足,還差 870 元 discountCalculation('Python', 142, 20, 30); //Python 您好,你所帶的金額不足,還差 1928 元 ``` *** ## 進階解答 - 利用 class 建立了店家模組可提供商品價格、優惠折扣 ```javascript= class Store { constructor(data) { this.friesPrice = data.friesPrice; this.hamburgerPrice = data.hamburgerPrice; this.sale = data.sale; this.getPrice = function(friesNum, hambNum) { return (this.friesPrice * friesNum + this.hamburgerPrice * hambNum) * this.sale; }; this.askPrice = function(name, friesNum, hambNum) { let price = this.getPrice(friesNum, hambNum); console.log(`${name} 您好,您詢問的金額總計為 ${price} 元`); }; this.balance = function(name, wallet, friesNum, hambNum) { var result = wallet - this.getPrice(friesNum, hambNum); if(result >= 0) console.log(`${name} 買完東西後,錢包剩下 ${result} 元`); else console.log(`${name} 您好,你所帶的金額不足,還差 ${Math.abs(result)} 元`); }; }; }; //第一題 var q1Store = new Store({ friesPrice: 40, hamburgerPrice: 50, sale: 1, }); q1Store.askPrice('Bob', 2, 2); //Bob 您好,您詢問的金額總計為 180 元 q1Store.askPrice('Mark', 10, 2); //Mark 您好,您詢問的金額總計為 500 元 q1Store.askPrice('Mary', 4, 7); //Mary 您好,您詢問的金額總計為 510 元 //第二題 var q2Store = new Store({ friesPrice: 40, hamburgerPrice: 50, sale: 1, }); q2Store.balance('Jack', 2000, 2, 7); //Jack 買完東西後,錢包剩下 1570 元 q2Store.balance('John', 3400, 8, 2); //John 買完東西後,錢包剩下 2980 元 q2Store.balance('Susan', 100, 5, 8); //Susan 您好,你所帶的金額不足,還差 500 元 //第三題 var q3Store = new Store({ friesPrice: 40, hamburgerPrice: 50, sale: 0.9, }); q3Store.balance('JavaScript', 1000, 10, 10); //JavaScript 買完東西後,錢包剩下 190 元 q3Store.balance('CSharp', 1200, 20, 30); //CSharp 您好,你所帶的金額不足,還差 870 元 q3Store.balance('Python', 142, 20, 30); //Python 您好,你所帶的金額不足,還差 1928 元 ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully