Visitar URL original
learned and finished day17-18 · soyaine/JavaScript30@6e4d335 · GitHub
Skip to content

Commit 6e4d335

Browse files
committed
learned and finished day17-18
1 parent 870aa50 commit 6e4d335

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎17 - Sort Without Articles/index-start.html‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,19 @@
4646
<script>
4747
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'];
4848

49+
function delPrefix(item){
50+
return item.replace(/^(The|A|An)\s{1}/,'');
51+
}
4952

53+
function displayArray(arr){
54+
document.getElementById('bands').innerHTML = '<li>'+arr.join('</li><li>')+'</li>';
55+
}
56+
57+
bands.sort(function(a,b){
58+
return delPrefix(a) > delPrefix(b) ? 1 : -1;
59+
});
60+
61+
displayArray(bands);
5062
</script>
5163

5264
</body>

‎18 - AddingUpTimesWithReduce/index-start.html‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<title>Videos</title>
66
</head>
77
<body>
8+
<p>total time:<span id="totaltime"></span></p>
89
<ul class="videos">
910
<li data-time="5:43">
1011
Video 1
@@ -182,6 +183,26 @@
182183
</li>
183184
</ul>
184185
<script>
186+
function getTime(){
187+
let times = [];
188+
let lists = document.getElementsByTagName('li');
189+
for(let i=0; i<lists.length; i++){
190+
let itemTime = lists[i].dataset['time'].split(':');
191+
times.push(parseInt(itemTime[0],10)*60 + parseInt(itemTime[1],10));
192+
}
193+
return times.reduce(function(a,b){return a+b},0);
194+
}
195+
196+
function formatTime(seconds){
197+
let sec = seconds % 60;
198+
let hour = Math.floor(seconds/3600);
199+
let min = (seconds - 3600*hour - sec)/60;
200+
document.getElementById('totaltime').innerHTML = `${hour}:${min}:${sec}`;
201+
}
202+
203+
let totalTime = getTime();
204+
formatTime(totalTime);
205+
185206
</script>
186207
</body>
187208
</html>

0 commit comments

Comments
 (0)