Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Antony .
Antony .
2,824 Points

Conditional statement is not adding the other songs.

Hi, I need help on figuring out how to add the the rest of the song names that aren't character length of 47 or more.

E.g.,

Songs:

  1. Ariana Grande - 7 rings
  2. Halsey - Without Me (Live From The Billboard Music Awards) //58 characters
  3. Jealous, Cake By The Ocean, Sucker Medley
  4. Madonna, Maluma - Medellรญn
  5. Taylor Swift - ME! (feat. Brendon Urie of Panic! At The Disco) //62

I need to add 3 dots after 47 characters have been reached on these songs: "Halsey - Without Me" and "Taylor Swift - Me", but still continue to add the rest of the songs to the playlist variable. I understand what I'm doing underneath inside the function but that's just a code example.

I apologize if i left something out, or something doesn't make any sense. I haven't slept for about 15 hours.

const {Client, Attachment, RichEmbed} = require('discord.js');
const client = new Client();
var scSearcher = require('soundcloud-searcher');

if(msg.content === config.cmd + "ssc") {
    // Initi with your clientId. https://soundcloud.com/you/apps
    scSearcher.init(config.soundcloud_api_key)
    var options = {
      name: '',
      limit: 5, genres: [scSearcher.genre.PKawaii],
      tags: ['P kawaii', 'P-Kawaii', 'P Kawaii', 'P-Kawaii!']
    }

    scSearcher.search(options)
      .then((result) => {
        const parsed = JSON.parse(result);
        let arr = [];
        for(i = 0; i < parsed.length; i++) {
            arr.push(parsed[i].permalink_url);
        }
        console.log(arr);

        const guild = client.guilds.get('380247493188780034');
        const guildOwner = guild.owner.nickname
        const embed = new RichEmbed()
        .setAuthor(guildOwner, parsed[0].user.avatar_url)
        .setTitle("Songs")
        .setDescription(songs())
        .setThumbnail("https://cdn2.iconfinder.com/data/icons/minimalism/512/soundcloud.png")

        msg.channel.send(embed);

        function songs() {
            let playlist = ``;
            for(i = 0; i < parsed.length; i++) {
                if(parsed[i].permalink.length >= 47) {
                    playlist += (parsed[i].permalink.substring(0, 47) + "...\n");
                } else {
                    playlist += (`${[i+1]}. ${parsed[i].permalink}\n`);
                }
            }
            return playlist;
          }

      }).catch((error) => {
        console.log(error)
      });
}

2 Answers

Steven Parker
Steven Parker
231,236 Points

It would help to see more of the context, but it looks like an argument of 0 is used in a couple of places where "i" should be instead. Also, I would expect to see an "else" so that only one of the statements that adds to "playList' is used.

For future questions, if you're using a workspace, you can make a "snapshot" and post the link as a convenient way to share the complete context of the code. Also, if this is for a course, a link to the course page would also be helpful.

Antony .
Antony .
2,824 Points

Yup I've replaced the 0 with [i]'s just now. Also I've updated the description. Let me know if you need more specific information!

Antony .
Antony .
2,824 Points

I also added a else statement, but the output looks like this:

  1. Ariana Grande - 7 rings

Halsey - Without Me (Live From The Billboard Music Awards) //doesn't have a number beside it

  1. Jealous, Cake By The Ocean, Sucker Medley
  2. Madonna, Maluma - Medellรญn

Taylor Swift - ME! (feat. Brendon Urie of Panic! At The Disco) //doesn't have a number beside it

Antony .
Antony .
2,824 Points

Updated the description again!

Steven Parker
Steven Parker
231,236 Points

You can add numbers to the truncated names similarly to how you do it on the others, but don't enclose the displayed number in brackets:

    if(parsed[i].permalink.length >= 47) {
        playlist += `${i+1}. ${parsed[i].permalink.substring(0, 47)}...\n`;
    } else {
        playlist += `${i+1}. ${parsed[i].permalink}\n`;
    }
Antony .
Antony .
2,824 Points

YUP, that's EXACTLY what i did. Hahaha. Thank you steven.