TypeScript: The Type Experiment

The Type Experiment: Can you spot the JavaScript errors?
const favorites = {
tracks: [],
totalLength: 0,
function addToFavorites({ title, artist, album, length }) {
favorites.tracks.add({ title, artist, album, length });
favorites.totalLength = favorites.totalLength + track.length;
const track = {
title: 'Bohemian Rhapsody',
artists: ['Queen'],
album: 'A Night At The Opera',
length: '5.45m',

The Solution

Yup! As expected, we do have a couple of issues in this code…
  • At line #7, we are using an array method that doesn't exist. The correct method is push() not add().
  • At line #8, we are adding a string '5.45m' to the number 0. Again, this is valid JavaScript code. The JavaScript compiler will automatically coerce the number to a string and the result will be '5.45m', which is far ahead of what we wanted to achieve here.
  • Ultimately, at line #13, we have a typo in the property name artist, which must not be plural.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nicos Tsourektsidis

Nicos Tsourektsidis

Lead Software Engineer @ Epam Systems Switzerland. I ❤️ JavaScript, React, User Interfaces, coffee and video games.