当前位置: 首页 > news >正文

typescript综合练习1(展开音乐播放列表)

Playlist Soundness

What’s up, friend?!
I’m so pumped you’re joining us.
We’ve got a sick project we could totally use your help on!

See, someone’s giving us amazing recommendations for songs to play.
But they’re not just coming in as songs.
Sometimes they’re an album containing a array of songs.
And sometimes they’re a playlist with a method that returns an array of songs.

We’d like you to write a function for us that takes in an array of those items and returns a result playlist.
The result playlist should keep track of which songs appear under each artist, the in-order list of songs, and the total length of time across the playlist?

Can you do this for us, please?
Friend?

Setup

In one terminal, run the TypeScript compiler via the tsc script.
For example, to start the TypeScript compiler in watch mode:

npm run tsc -- --watch

In another terminal, run Jest via the test script.
For example, to start tests in watch mode:

npm run test -- --watch

Specification

Your code should export an unrollPlaylist function with an explicit return type annotation.

Parameters:

  1. items: An array where each element is either a Song, Album, or Playlist

    See ./index.test.ts for examples of data.

Return type: an object with…

  • artists: Object with artist names keyed to the array of songs they’re credited on
  • songs: An array of Songs
  • time: Total length of time across all songs

Notes

  • Don’t import code from one step into another.

代码

export interface Song {artist: string | string[];length: number;name: string;type: "song";
}export interface Album {songs: Song[];type: "album";
}export interface Playlist {resolve(): Song[];type: "playlist";
}export type PlaylistItem = Album | Song | Playlist;export interface Artists {[i: string]: string[];
}export interface UnrolledPlaylist {artists: Artists;songs: string[];time: number;
}export function unrollPlaylist(items: PlaylistItem[]): UnrolledPlaylist {const artists: Artists = {};const songs: string[] = [];let time = 0;function addSong(song: Song) {const songArtists =typeof song.artist === "string" ? [song.artist] : song.artist;for (const artist of songArtists) {artists[artist] ??= [];artists[artist].push(song.name);}time += song.length;songs.push(song.name);}for (const item of items) {switch (item.type) {case "song":addSong(item);break;case "album":item.songs.forEach(addSong);break;case "playlist":item.resolve().forEach(addSong);break;}}return { artists, songs, time };
}let items : PlaylistItem[] = [{songs: [{artist: ["Queen"],length: 223,name: "Death On Two Legs (Dedicated to...)",type: "song",},{artist: "Tenacious D",length: 247,name: "Tribute",type: "song",},{artist: ["Queen"],length: 68,name: "Lazing on a Sunday Afternoon",type: "song",},],type: "album",},
]let unrolledPlaylist: UnrolledPlaylist = unrollPlaylist(items)
console.log(unrolledPlaylist)

输出

{artists: {Queen: ['Death On Two Legs (Dedicated to...)','Lazing on a Sunday Afternoon'],'Tenacious D': [ 'Tribute' ]},songs: ['Death On Two Legs (Dedicated to...)','Tribute','Lazing on a Sunday Afternoon'],time: 538
}
http://www.lryc.cn/news/344411.html

相关文章:

  • 零基础入门学习Python第二阶02面向对象,迭代器生成器,并发编程
  • Unity | Shader基础知识(第十三集:编写内置着色器阶段总结和表面着色器的补充介绍)
  • JavaScript map对象/set对象详解
  • 【kettle017】kettle访问DB2数据库并处理数据至execl文件(最近完善中)
  • Spring Cloud原理详解和作用特点
  • Linux —— 进程间通信
  • ASP.NET信息安全研究所设备管理系统的设计与实现
  • <网络安全>《81 微课堂<安全产品微简介(1)>》
  • 【6D位姿估计】FoundationPose 跑通demo 训练记录
  • Python 中 “yield“ 的不同行为
  • 迅睿CMS中实现关键词搜索高亮
  • 晶振的精度与稳定性有什么关系?
  • 【C】137 只出现一次的数字
  • 51单片机入门:DS1302时钟
  • Redis-5 分布式锁
  • 音转文工具,9.8k star! 【送源码】
  • 【首次发布】华为 OD 机试 C卷抽中题库清单(真题库),目前华为OD机考以C卷为主,特殊情况会发送D卷
  • 【进程等待】waitpid的参数pid | status的位图位操作WIFEXITEDWEXITSTATUS宏
  • unity---常用API
  • 设计模式: 模板模式
  • [虚拟机+单机]梦幻契约H5修复版_附GM工具
  • 头文件相互包含 前向声明
  • 七款好用的上网行为管理软件推荐 |有没有好用的上网行为管理系统
  • centos7-bcc 安装
  • 5.06号模拟前端面试8问
  • 解读Inscode AI:开启代码智能化的新时代
  • 快速了解Vuex
  • vue管理系统导航中添加新的iconfont的图标
  • Docker的介绍及与传统虚拟化技术的区别
  • 06.Git远程仓库