Mixed Messages: Bad Advice generator

Howdy yall, newcomer to JS and coding in general. Thought it might be fun to make a ‘bad advice generator’ for this project. I’ll post the code and the repo link below!

`// For my version of the Mixed Messages Codecademy project, I'm going to create 

// a bad advice generator. It's just going to dole out bad, randomly generated advice to the user. 

// it will output a format string with 3 peices of data in it, all randomly selected from a list of words

// I will also include a 'Name your firstborn child ${literalRandomLetters}', because why not have a 

// legitimately randomly generated portion in there?

const verbs = ['Give', 'Sell', 'Move', 'Take', 'Burn', 'Inhale'];

const people = ['child', 'neighbor', 'cousin', 'father', 'grandparents'];

const objects = ['drugs', 'a timeshare', '100 lottery tickets', 'books'];

console.log(Math.floor(Math.random() * 5));

console.log(verbs[Math.floor(Math.random() * 5)]);

let wordOne = verbs[Math.floor(Math.random() * 5)];

let wordTwo = people[Math.floor(Math.random() * 4)];

let wordThree = objects[Math.floor(Math.random() * 3)];

if (wordOne === 'Move' || wordOne === 'Burn' || wordOne === 'Take' || wordOne === 'Inhale'){

    if (wordTwo === 'grandparents'){

        wordTwo = wordTwo + '\''

    }else{

    wordTwo = wordTwo + '\'s';

}

    if (wordThree === 'a timeshare'){

        wordThree = wordThree.replace('a timeshare', 'timeshare');

    }

}

let badAdvice = `${wordOne} your ${wordTwo} ${wordThree}`;

console.log(badAdvice);`

https://github.com/NoahOAHickman/CODING-PROJECTS.git

Hello, and welcome to the forums!

You can format your code for the forums by following the advice in this thread: [How to] Format code in posts

Also, the GitHub link you posted doesn’t work. There doesn’t appear to be any public repositories on that account either, so if it was a private repo then we won’t be able to see it.

oof, thanks for the corrections!

Great, the formatting looks good and the GitHub link works now!

There’s an issue with the range of random numbers being generated. No matter how many times the program runs, the bad advice will never include “Inhale your grandparents’ books”.

Math.random() generates a number between 0 and 1, but never exactly 1. So when you multiply that by a number and then use Math.floor(), the maximum value will always be 1 less than the number you used to multiply Math.random()

What that means is Math.floor(Math.random() * 5) will only ever be able to produce the numbers 0, 1, 2, 3, and 4. It will never be able to produce the number 5.

So if an array has 6 elements, like your verbs array, then that’s the number you want to use so that it can create a random index of 0, 1, 2, 3, 4, or 5.

Also, instead of hardcoding the number, it would be a good idea to use the array property length so that if you add or remove elements from the array, you never have to worry about updating the values in other places.

Example:

let wordOne = verbs[Math.floor(Math.random() * verbs.length)];

Making that correction throughout your code will allow all options to be randomly chosen. From burning books to inhaling lottery tickets, the bad advice will flow without limits.

Your project idea was a fun concept. Good work

1 Like

Hey thanks for the advice! that makes a ton of sense, I really appreciate it! I’ll make the change!