Repeating song in a playlist coding test in Javascript

I’m doing practice coding interview tests and can’t for the life of me figure out why my solution isn’t passing.

I know this is a dupe of this, and that a python version of it is here, but I’m pretty sure my issue is one of javascript internal workings and less of algorithmic design (or maybe not). Either way, I didn’t want to hijack that dupe (it was unanswered anyway) as my question is more specific.

The prompt:

A playlist is considered a repeating playlist if any of the songs
contain a reference to a previous song in the playlist. Otherwise, the
playlist will end with the last song which points to null. Implement a
function isRepeatingPlaylist that, returns true if a playlist is
repeating or false if it is not. For example, the following code
prints “true” as both songs point to each other:

Code starting template:

class Song {
    name;
    nextSong;
    
    constructor(name) {
      this.name = name;
    }
  
    /**
     * @return {boolean} true if the playlist is repeating, false if not.
     */
    isRepeatingPlaylist() {
      // Your code goes here
    }
  }
  
let first = new Song("Hello");
let second = new Song("Eye of the tiger");
  
first.nextSong = second;
second.nextSong = first;
  
console.log(first.isRepeatingPlaylist());

My solution thus far. Expanded with console.log and additional songs to help me figure out what is going on. I’ve been running it using node in my terminal. It appears that my issue is in the line current = this.nextSong;, it just does not seem to be reassigning to the next object correctly. Thoughts?

class Song {
  name;
  nextSong;
  
  constructor(name) {
    this.name = name;
  }
  
  /**
   * @return {boolean} true if the playlist is repeating, false if not.
   */
  isInRepeatingPlaylist() {
    // Your code goes here
    let playlist = [];
    let current = this;
    while (current.name && current.nextSong) {
      console.log('playlist:', playlist);
      if (playlist.includes(current.name)) {
        console.log('in playlist')
        return true;
      } else {
        console.log('not in playlist')
        playlist.push(current.name)
        console.log('added:', current.name)
        console.log('next up:', current.nextSong)
        current = this.nextSong;
        console.log('next up assigned?:', current)
      }
    }
    return false;
  }
}

let first = new Song("Hello");
let second = new Song("Eye of the tiger");
let third = new Song("Third");
let fourth = new Song("Fourth");

first.nextSong = second;
second.nextSong = third;
third.nextSong = fourth; //not a repeating playlist, should return false
// fourth.nextSong = first;

console.log(first.isInRepeatingPlaylist());
// true

and my output:

playlist: []
not in playlist
added: Hello
next up: Song {
  name: 'Eye of the tiger',
  nextSong: Song {
    name: 'Third',
    nextSong: Song { name: 'Fourth', nextSong: undefined }
  }
}
next up assigned?: Song {
  name: 'Eye of the tiger',
  nextSong: Song {
    name: 'Third',
    nextSong: Song { name: 'Fourth', nextSong: undefined }
  }
}
playlist: [ 'Hello' ]
not in playlist
added: Eye of the tiger
next up: Song {
  name: 'Third',
  nextSong: Song { name: 'Fourth', nextSong: undefined }
}
next up assigned?: Song {
  name: 'Eye of the tiger',
  nextSong: Song {
    name: 'Third',
    nextSong: Song { name: 'Fourth', nextSong: undefined }
  }
}
playlist: [ 'Hello', 'Eye of the tiger' ]
in playlist
true

1

current = this.nextSong; keeps pointing to the same old song. Also, you can and should compare the objects directly instead of their name property.

class Song {
  name;
  nextSong;

  constructor(name) {
    this.name = name;
  }

  /**
   * @return {boolean} true if the playlist is repeating, false if not.
   */
  isInRepeatingPlaylist() {
    // Your code goes here
    let playlist = [];
    let current = this;
    while (current && current.nextSong) {
      if (playlist.includes(current)) {
        return true;
      }
      playlist.push(current)
      
      // this was your error:
      current = current.nextSong;
    }
    return false;
  }
}

let first = new Song("Hello");
let second = new Song("Eye of the tiger");
let third = new Song("Third");
let fourth = new Song("Fourth");

first.nextSong = second;
second.nextSong = third;
third.nextSong = fourth; //not a repeating playlist, should return false
// fourth.nextSong = first;

console.log(first.isInRepeatingPlaylist());
// true

A playlist is considered a repeating playlist if any of the songs contain a reference to a previous song in the playlist

  • A playlist containing two songs with the same name is not considered a repeating playlist, so using name is a bad idea.
  • You seems to be mistaken a while loop with a recursive version because this in current = this.nextSong is referred to first every loop.
  • This has to be a set problem, I’m using weakset to store the song reference.
class Song {
  name;
  nextSong;

  constructor(name) {
    this.name = name;
  }

  /**
   * @return {boolean} true if the playlist is repeating, false if not.
   */
  isRepeatingPlaylist() {
    const playedSongs = new WeakSet();
    playedSongs.add(this);
    let nextSong = this.nextSong;
    while (nextSong) {
      if (playedSongs.has(nextSong)) return false;
      playedSongs.add(nextSong);
      nextSong = nextSong.nextSong;
    }
    return true;
  }
}

let first = new Song('Hello');
let second = new Song('Eye of the tiger');

first.nextSong = second;
second.nextSong = first;

console.log(first.isRepeatingPlaylist());

Working sample: https://stackblitz.com/edit/vitejs-vite-vmyf8u?file=main.js&terminal=dev

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật