Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                // -------- 公式 --------
// BMI 值的計算公式為:體重 / 身高^2。

// -------- 題目 --------
// 假設體重是 70,身高是 180(1.8m),BMI 就是 70/(1.8*1.8) = 21。
// 現在請你寫出一個簡單的 BMI 計算器,用兩個變數代表體重跟身高,算出 BMI 之後判斷 BMI 是落在哪個範圍內並輸出相對應的字串。

let yourHeight = 150 // 輸入身高,單位:cm
let yourWeight = 43  // 輸入體重,單位:kg

let BMI = {
  enterValue: function (height, weight) {
    if ( isNaN(weight) ||  weight > 200 || weight < 0) {
      console.log('體重要填數字...還有不可以騙人')
    } else if ( isNaN(height) || height > 220 || height < 0) {
      console.log('身高要填數字...還有不可以騙人')
    } else {
      const meterHeight = height / 100
      const value = weight / (meterHeight * meterHeight)
      this.result(value)
    }
  },
  result: (data) => {
    // -------- 範圍 --------
    // 體重過輕:BMI < 18.5
    // 正常範圍:18.5 <= BMI < 24
    // 過重:   24   <= BMI < 27
    // 輕度肥胖:27   <= BMI < 30
    // 中度肥胖:30   <= BMI < 35
    // 重度肥胖:35   <= BMI
    if (data < 18.5 ) {
      console.log('體重過輕')
    } else if (data >= 18.5 && data < 24) {
      console.log('正常範圍')
    } else if (data >= 24 && data < 27) {
      console.log('輕度肥胖')
    } else if (data >= 27 && data < 30) {
      console.log('中度肥胖')
    } else if (data >= 30 && data < 35) {
      console.log('重度肥胖')
    }
  }
}

BMI.enterValue(yourHeight, yourWeight) // 在這裡輸入身高體重
              
            
!
999px

Console