(It’s not really the build-a-library project but there was no selector for the Javascript Syntax Portfolio Project)
So I’ve been running into issues with Javascript and could really use the help. I feel like I must be missing a piece of the puzzle bc I am pretty lost ngl.
Essentially, this code aims to randomly select/give a joke from 5 arrays. I have individual getters for the arrays that are working just fine and successfully randomly pick/give a random joke from their respective arrays.
A different array, _jokes[]
, nests the other 5 within it. randomJoke() is a method that accesses this array and attempts to return a single joke from one of the 5 nested arrays. But I am ending up with a lot of confusing typeerrors. And idk why it’s not working. I’ve tried the following code:
randomJoke() {
let categoryIndex = Math.floor(Math.random() * this._jokes.length);
let category = this._jokes[categoryIndex];
let jokeIndex = Math.floor(Math.random() * category.length);
return category[jokeIndex];
}
I’ve also tried:
randomJoke() {
let i = Math.floor(Math.random() * this._jokes.length);
let j = Math.floor(Math.random() * this._jokes[i].length);
return this._jokes[i][j];
}
Unfortunately, both return typeerrors. Here’s the full code:
const objJokes = {
_edgyJokes: ['I told my psychiatrist I had suicidal thoughts. He said from now on, I have to pay in advance.',
'Why did the hipster burn his tongue? Because he drank his coffee before it was cool.',
'Why did the scarecrow win an award? Because he was outstanding in his field, unlike the rest of us losers.',
'I tried to sue the airport for losing my luggage. I lost my case.',
"I asked the librarian if the library had any books on paranoia. She whispered, 'They're right behind you.'"],
_dadJokes: ['What do you call fake spaghetti? An impasta!',
"Why don't skeletons fight each other? They don't have the guts!",
'I told my wife she should embrace her mistakes. She gave me a hug.',
'Did you hear about the kidnapping at the playground? They woke up.',
"Why don't eggs tell jokes? They'd crack each other up!"],
_oldJokes: ['I told my wife she was getting old. She went to go get a second opinion and never came back.',
'I told my doctor I broke my arm in two places. He told me to stop going to those places.',
"I don't need a hairstylist. My pillow gives me a new hairstyle every morning.",
"I asked my grandpa how he's feeling. He said, 'With my hands.'",
"I asked my grandma if she ever tried walking in my shoes. She said, 'No, but I've tried kicking you out of them!'"],
_religionJokes: ['What do you call a gathering of atheists? A non-prophet organization!',
"Why don't atheists solve exponential equations? Because they don't believe in higher powers!",
"Why did the atheist go to church? To catch some z's during the sermon!",
'How many atheists does it take to change a light bulb? None, they prefer to see the light of reason!',
"Why don't atheists attend church bake sales? Because they have a 'no prophet' margin policy!"],
_kidJokes: ['Why did the scarecrow win an award? Because he was outstanding in his field!',
'What did one ocean say to the other ocean? Nothing, they just waved!',
'Why did the bicycle fall over? Because it was two-tired!',
"What's orange and sounds like a parrot? A carrot!",
"What do you call cheese that isn't yours? Nacho cheese!"],
_jokes: [this._edgyJokes, this._dadJokes, this._oldJokes, this._religionJokes, this._kidJokes],
get edgyJoke() {
let i = Math.floor(Math.random() * this._edgyJokes.length);
return this._edgyJokes[i];
},
get dadJoke() {
let i = Math.floor(Math.random() * this._dadJokes.length);
return this._dadJokes[i];
},
get oldJoke() {
let i = Math.floor(Math.random() * this._oldJokes.length);
return this._oldJokes[i];
},
get religionJoke() {
let i = Math.floor(Math.random() * this._religionJokes.length);
return this._religionJokes[i];
},
get kidJoke() {
let i = Math.floor(Math.random() * this._kidJokes.length);
return this._kidJokes[i];
},
// THIS METHOD IS THE PROBLEM CHILD
randomJoke() {
let categoryIndex = Math.floor(Math.random() * this._jokes.length);
let category = this._jokes[categoryIndex];
let jokeIndex = Math.floor(Math.random() * category.length);
return category[jokeIndex];
}
};
The test `console.log()`s:
// FOR CATEGORICAL JOKES
/*
console.log(objJokes.edgyJoke);
console.log(objJokes.dadJoke);
console.log(objJokes.oldJoke);
console.log(objJokes.religionJoke);
console.log(objJokes.kidJoke);
*/
//FOR A SINGLE RANDOM JOKE
console.log(objJokes.randomJoke());
Thank you for taking a look! Hoping I can get this figured out.