Mixed Messages - Seeking Feedback!

Hello Everyone!

This is my first post in the forums, so I’m a little bit nervous to get some feedback. I just finished the Mixed Messages project as a part of the Full Stack Developer Career Path. I was suprised that this project only took me about 2 hours and 30 minutes, give or take. It took me about an hour to lay out the foundation of the script, 30 minutes to input all my different sentences and phrases, and another hour to work my way around gitHub and setting up a README.md.
I’ll include the link to my repository here, and I’ll also paste the code below for ease of use.

Any and all feedback is much appreciated.

let genres = [
    "Pop",
    "Rock",
    "Country",
    "Jazz",
    "Techno",
    "Classical",
    "Metal",
    "Funk",
    "Hip Hop",
    "Rap"
];
let songSuggestion = [
    ["'Rain on Me' - Lady Gaga", "'Positions' - Ariana Grande", "'Bad Guy' - Billy Eilish"],
    ["'Bohemian Rhapsody' - Queen", "'Baba O'Riley' - The Who", "'Back in Black' - AC/DC"],
    ["'Life is a Highway' - Rascall Flatts", "'Jolene' - Dolly Parton", "'Ring of Fire' - Johnny Cash"],
    ["'Giant Steps' - John Coltrane", "'Take 5' - Manhattan Transfer", "'The Girl from Ipanema' - Stan Getz"],
    ["'Sandstorm' - Darude", "'I Wanna Go Bang' - Bjarki", "'Subzero' - Ben Klock"],
    ["'A Soft Day' - Syracuse University Singers", "'Lux Aeterna: III O Nata Lux' - Syracuse University Singers", "'Amazing Grace' - Syracuse University Singers"],
    ["'Hallowed Be Thy Name' - Iron Maiden", "'Angel of Death' - Slayer", "'Crazy Train' - Ozzy Osbourne"],
    ["'Super Freak' - Rick James", "'1612' - Vulfpeck", "'Shining Star' - Earth, Wind & Fire"],
    ["'Savage' - Megan Thee Stallion", "'WAP' - Cardi B", "'Laugh Now Cry Later' - Drake"],
    ["'Tang Clan' - Wu-Tang Clan", "'Dear Mama' - Tupac Shakur", "'Passin Me By' - The Phyarcyde"],
    ["'Man in the Mirror' - Michael Jackson", "'Wait a Minute' - Willow Smith", "'Welcome to Burlesque' - Cher"]
];

let genreStereotype = [
    "POPular kid",
    "Hard rocker",
    "Country music buff",
    "Scat Master",
    "Techno-logist",
    "diva",
    "Metalhead",
    "vibe curator",
    "Hiphopotamuse",
    "Rhyme Rinoceroce",
];

var d = new Date();
var n = d.getHours();

if (n >= 0 && n <= 12){
    n = "Morning"
}
if (n > 12 && n <= 14){
    n = "Afternoon"
}
if (n > 6 && n <= 23){
    n = "Evening"
}
//console.log(n)
var day = d.getDay();
if (day === 0){
    day = "Sunday"
}
if (day === 1){
    day = "Monday"
}
if (day === 2){
    day = "Tuesday"
}
if (day === 3){
    day = "Wednesday"
}
if (day === 4){
    day = "Thursday"
}
if (day === 5){
    day = "Friday"
}
if (day === 6){
    day = "Saturday"
}
//console.log(day)

const mixedMessages = (userName, userGenre) => {
    for (let i = 0; i < genres.length; i++){
        if (userGenre == genres[i]){
            console.log(`Hello, ${userName}! I see that you like listening to ${genres[i]}!`);
            let index = genres.indexOf(genres[i])
            let randomSong = songSuggestion[index][Math.floor(Math.random() * songSuggestion[index].length)]
            console.log(`If you like ${userGenre}, you might like this song: ${randomSong}.`);
            console.log(`You're a true ${genreStereotype[index]} if you like listening to ${userGenre} on a ${day} ${n}!`);    
        break
        } else { 
            console.log(`Hi ${userName}! I actually haven't heard of ${userGenre} before! 
        Can you tell me your second favorite genre? I figured in the meantime I could share one of my favorite songs with you anyways, 
        its ${songSuggestion[10][Math.floor(Math.random() * songSuggestion[10].length)]}.`)     
        }
        break
    }
};

@jessescheinbart17574
Great work and welcome to the forums! :boom:
take these few hints to go forward :muscle::

var d = new Date();

for many reasons stop using var just use let or const instead. Also, it’s recommended to give your variable a descriptive name. date is more readable than d and hours instead of n and so on.

==========================

if (n >= 0 && n <= 12){
    n = "Morning"
}
if (n > 12 && n <= 14){
    n = "Afternoon"
}
// n> 14 not n > 6 i think it's just a typo so fix it.
if (n > 6 && n <= 23){
    n = "Evening"
}

This approach is right but it will make more sense to use if..else if because the three ifs are related to each other, just imagine the n = 13 so it will be only “Afternoon”. so chaining the three ifs together will make more sense.
so you can change it to

if (n >= 0 && n <= 12){
    n = "Morning"
}
else if (n > 12 && n <= 14){
    n = "Afternoon"
}
else {
    n = "Evening"
}

==================================
And about the 7 days ifs, you also can convert them to if..else if, but I think it will be easier and more readable to use switch here because we are checking the value of one variable:

switch (day) {
  // case 0 means day === 0
  case 0:
    day = "sunday"
    break;
  case 1:
    day = "monday"
    break;  

  // ....and so on use case for all days

  default:
    // default here will be day 6
    day = "saturday"
    break;
}

===========================

let index = genres.indexOf(genres[i])

why are you trying to get the index of this array, you already have the index value in i, so just remove this line and use i instead of index because they are the same.

==============================

   break
    }
};

this last(second) break will break your loop and I didn’t get the reason for it, I think you should remove it.

==============================
You are doing great work! keep going. :boom:

Hi @enghosamokasha, this feedback is incredibly helpful, as its the first time anybody has laid an eye on my code. I’ll appreciate the time you have taken to giving me this feedback. I would love to know more about why you said to not use var, and to use ‘let’ or ‘const’ instead. What are the benefits of doing this, or is it an industry standard?

1 Like

I’m not sure if it will be an advanced topic for you at this level or not, just try to read this article and I hope you will understand why let and const better than var.

1 Like

Thank you again, @enghosamokasha I read the article and think this will help my writing going forward. :slight_smile: