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 trialAntony .
2,824 PointsConditional 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:
- Ariana Grande - 7 rings
- Halsey - Without Me (Live From The Billboard Music Awards) //58 characters
- Jealous, Cake By The Ocean, Sucker Medley
- Madonna, Maluma - Medellรญn
- 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
231,236 PointsIt 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 .
2,824 PointsSolved!!
Antony .
2,824 PointsAntony .
2,824 PointsYup 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 .
2,824 PointsAntony .
2,824 PointsI also added a else statement, but the output looks like this:
Halsey - Without Me (Live From The Billboard Music Awards) //doesn't have a number beside it
Taylor Swift - ME! (feat. Brendon Urie of Panic! At The Disco) //doesn't have a number beside it
Antony .
2,824 PointsAntony .
2,824 PointsUpdated the description again!
Steven Parker
231,236 PointsSteven Parker
231,236 PointsYou 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:
Antony .
2,824 PointsAntony .
2,824 PointsYUP, that's EXACTLY what i did. Hahaha. Thank you steven.