語法練習
請進入 (1) nodejs 環境, 或 (2) firebug 的主控台, 或 (3) chrome 的 「開發人員工具」 以便執行以下指令。 也請參考 Firebug 與 Chrome 的 console 基本操作。
一、 數字
7*6
50/6
Math.floor(50/6)
Math.ceil(50/6)
Math.sqrt(2)
Math.PI
Math.atan2(1, 1)/Math.PI*180
二、 字串
'hello' + 'world'
"hello" + "world"
x = '朝陽科技大學資訊管理系'
x.length
x.charAt(0)
x.charAt(4)
x.substring(4,6)
x.indexOf('科技大學')
x.indexOf('查無此人')
x.charCodeAt(0)
'hello world'.toUpperCase()
'hello'.charCodeAt(1)
String.fromCharCode(65)
三、 陣列
primes = [2,3,5,7,11]
primes[0]
primes.length
// Q: 如何取得陣列最後一個元素?
primes.indexOf(11)
primes.indexOf(10)
primes.concat([13, 17, 19, 23])
primes // 沒變!
y = primes.concat([13, 17, 19, 23])
y.slice(4,7)
以下函數會改變陣列的內容!
右圖取自
JavascriptTutorial。
primes.reverse()
primes // 內容改變了!
primes.pop()
primes // 尾巴被砍掉了
primes.push(97)
primes // 長出新尾巴
primes.shift()
primes // 頭被砍掉了
primes.unshift(101)
primes // 長出新頭
primes = [2,3,5,7,11,13,17,19,23]
primes.splice(4, 2, 'a', 'b', 'c', 'd')
primes
四、 轉換
parseInt("507.32")
parseFloat("507.32")
x = 507.32
x.toString()
x = 'hello world'.split('')
x.join('')
x = 'i have a dream'
x.split(' ')
x.split(' ').join(':')
'i have a dream'.split('')
'i have a dream'.split(/ +/)
作業: 一句話反轉一個字串。 提示: 轉成陣列、 反轉、 再轉回字串。
- 本頁最新版網址: https://frdm.cyut.edu.tw/~ckhung/b/js/syntax.php; 您所看到的版本: June 25 2016 09:00:42.
- 作者: 朝陽科技大學 資訊管理系 洪朝貴
- 寶貝你我的地球, 請 減少列印, 多用背面, 丟棄時做垃圾分類。
- 本文件以 Creative Commons Attribution-ShareAlike License 或以 Free Document License 方式公開授權大眾自由複製/修改/散佈。
![[rss feed 圖案]](/~ckhung/i/rss.png)