- 17-Sorting Band Names without articles 綜合練習
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("")
- 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);