Codecademy Project

- Mixed Messages


Project Requirements

For this project, you will build a message generator program. Every time a user runs a program, they should get a new, randomized output. You’re welcome to take the project in a couple of different forms, like an astrology generator, inspirational message, or nonsensical jokes. To make your program truly random, the message that it outputs should be made up of at least three different pieces of data. Take what you know of JavaScript syntax so far to build the program and customize it to your liking.

Project Objectives:

  • Build a message generator program using JavaScript
  • Use Git version control
  • Use command line
  • Develop locally on your computer
Prerequisites:
  • JavaScript
  • Git and GitHub
  • Command line

See it in action!

Click on "Inspire Me" to generate a random quote, a random famous person, random year and a random boolean.




JavaScript Code

    // Generates a random saying, random author, random date and random boolean
    function mixedM() {
        // Array contaying a list of sayings
    let sayingArray = [
        '\"The greatest glory in living lies not in never falling, but in rising every time we fall.\"',
        '\"The way to get started is to quit talking and begin doing.\"',
        '\"Life is what happens when you are busy making other plans\"',
        '\"If life were predictable it would cease to be life, and be without flavor.\"',
        "\"If you look at what you have in life, you'll always have more. If you look at what you don't have in life, you'll never have enough.\"",
    ];

        //Array containing a list of famous people who have said one of the sayings
    let authArray = [
        'Nelson Mandela',
        'Walt Disney',
        'John Lennon',
        'Eleanor Roosevelt',
        'Oprah Winfrey',
    ];

        // Array of boolean values to be chosen from at random
    let boolArray = [
        true,
        false
    ]
        // function that takes in an array and returns the value at a random index
    function rndChoice(arr) {
        let index = Math.floor(Math.random() * arr.length);
        return arr[index];
    }

        // select a random number to 1-754
        // this will be the number of years to take off the current year (today)
    let rndDate = Math.floor(Math.random() * 754)

        // The current year, eg:2021    
    let today = new Date().getFullYear();
        // variables to store each random choice
    let chosenSaying = rndChoice(sayingArray);
    let chosenAuth = rndChoice(authArray);
    let chosenBool = rndChoice(boolArray);

        // calculate a random year by deducting a random number from current year
    let chosenDate = today - rndDate;

        // determine the text displayed depending on if true or false
        // is chosen at random
    if (chosenBool === true) {
        boolText = "I can't believe this could be "
    } else {
        boolText = "I'm pretty sure this is "
    }

        // manipulate elemenst in the dom to display variables
    document.getElementById('saying').innerText = chosenSaying
    document.getElementById('authAndDate').innerText = `${chosenAuth} said this in the year ${chosenDate}`
    document.getElementById('bool').innerText = boolText + chosenBool + "!!";
    document.getElementById('block').style.opacity = "1";
}