Mixed Messages: Inspirational Quotes

Hi Comrades,
I did a js terminal app that provides random sentences about life, studying and a quote of a famous person.

  • It took me last evening and this morning to complete the project. The project turned to be easier than I initially thought. But I did not do any html, only the js part.
  • For the life and studying parts I used simple arrays to store the randomly changed parts. For the inspirational quotes I used an array of objects, with the author and the saying itself. So it was nice to practice referencing quotes in the array of objects, first time for me working with such data structure.
  • This was my first project using git responsibly. Previously I just followed prompts in Visual Studio Code. This time I followed the instructions from the course. During the work learned how to delete a remote branch ($ git push origin --delete remote_branch_name).
  • Here is the link to my github repo. I will be happy to receive your feedback!
    https://github.com/mlatysheva/codecademy_mixedMessages
    All the best,
    Maria
1 Like

Hi,

I really liked your idea. I thought I'd simplify your code a little, see what you think below :)

The randomElement function takes an array in its parentheses and returns a random element from that particular array using the arrays own length. It means you have less variables to declare and it reduces the code in the createMessage function a bit :) Also at the end, you don't have to create the message variable, just console.log the finalMessage, call the createMessage function, then call the mixedMessages function outside.


James

function randomElement(array) {
return array[Math.floor(Math.random()*array.length)];
}

function createMessage() {
    const lifeMessage = `Life is not always ${randomElement(lifeArray)}.`;
    const studyingMessage = `Studying requires ${randomElement(studyingArray)}.`;
    const sayingMessage = `As ${randomElement(sayingsObjArray)['author']} once said, 
    "${randomElement(sayingsObjArray)['saying']}"`;
    const finalMessage = `${lifeMessage} \n${studyingMessage} \n${sayingMessage}`;
    
    console.log(finalMessage);
}
createMessage(); 

}
mixedMessages();

1 Like

Hi James,
Thanks a lot for taking your time and reviewing my code! I really appreciate this.
I like your proposal of using a helper function. It definitely makes the code cleaner!
As for the last part of the main function. I somehow find it more practical when a function returns something rather than just consoles.log the result. So I will use return statement instead of the console.log.
Thanks a lot again and keep cool! :smile:
Maria

1 Like

Hi,

I’ve done a really similar consol program, also called it inspirational quotes, and from famous people too.
Interesting to see that our approaches were so different.

Regards,
Gergely

Hi Gergely,
Thanks a lot for sharing this!
It is funny that we chose such a similar topic! I would not say though that our approaches are that different. It’s the same logic, in my view.
I am currently switched to a short Udacity court on Javascript Promises and will be back to codecademy in a couple of days.
All the best,
Maria