iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 3
0
Modern Web

師父領進門 修行在個人系列 第 13

JS挑戰–(9)- map + reduce!

  • 分享至 

  • xImage
  •  
  1. 17-Sorting Band Names without articles 綜合練習
  • /^(a |the |an )/i
const bands = ['The Plot in You', 'The Devil Wears Prada', 'Pierce the Veil', 'Norma Jean', 'The Bled', 'Say Anything', 'The Midway State', 'We Came as Romans', 'Counterparts', 'Oh, Sleeper', 'A Skylit Drive', 'Anywhere But Here', 'An Old Dog'];

function strip(text) {
  return text.replace(/^(a |the |an )/i,"").trim()
}

const sortedBands = bands.sort((a,b) => strip(a)>strip(b)? 1:-1)

document.querySelector('#bands').innerHTML = sortedBands.map(band => `<li>${band}</li>`).join("")

  1. 18-Tally strings time with reduce
  • "5:12" split(':') 之後是 "5" "12" 先parseFloat轉為數字
  • / vs %
const timeNodes = [...document.querySelectorAll('[data-time]')]
const seconds = timeNodes
          .map(node => node.dataset.time)
          .map(timeCode => {
            const [mins, secs] = timeCode.split(":").map(parseFloat) //str -> number
            return mins * 60 + secs
          })
          .reduce((total, seconds) => total + seconds)  //秒數總和

          let secondsLeft = seconds
          const hour = Math.floor(secondsLeft/ 3600)
          const minute = Math.floor(secondsLeft % 3600 / 60)
          const second = ((secondsLeft % 3600 ) % 60)

console.log(hour, minute, second);

上一篇
JS挑戰-(8)- LocalStorage + Mouse move CSS
下一篇
JS挑戰—(10)— 影像 & 語音辨識
系列文
師父領進門 修行在個人30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言