Hello! I had some difficulty with this project as I am having difficulty with JS in general. I understand the individual concepts just fine but putting them together is an issue.
Anyway, check out my project and tell me what you think!
Hello, py7619621349!
Your code doesn’t seem to work.
I’d like to share a few thoughts about it:
- So, you have 4 separate arrays with an idea to combine them in order to get a random message.
Depending on the element chosen in the first array the script should then use either the second or the third array for the following phrase generation: if ‘I’ was chosen in noun array, then you use adverbB, else - adverbA. - Then, you created a function userChoice(noun) that takes one argument from user.
- Then a messy thing comes out. You use a for loop with 3 other for loops nested in it. The nested for loops do not process any code at all.
The function doesn’t return any value, it only logs a message. And you are using console.log again by calling your function.
The solution is more simple than it seems:
- So, we want to randomly take 1 array element from noun array.
1.1. We can get the length of the array and generate a random number from 0 to array.length - 1:
let randomNumber1 = Math.floor(Math.random() * noun.length)
1.2. Then we take the element from this array:
let randomArrayElement1 = noun[randomNumber]; - Then if this element is “I”, we use the 3d array for the further generation, else - we use the 2nd
array. We can use if conditional for this purpose:
if (randomArrayElement1 === ‘I’) {
} else {
}
The logic for generating a random element inside this if statement is the same as I described in 1.1 and 1.2. And so is the logic for generating the ending of your message from ‘verb’ array. - There is no need in having an argument for your function, as you can see. It is simply userChoice()
- When you have all your elements generated you are ready to combine a unique message with the help of the following line:
console.log(${randomArrayElement1} ${randomArrayElement2} more ${randomArrayElement3} than ${randomArrayElement1} know!
) - At last, in order to log a random message in your console you simply call your function:
userChoice();
(I mean, you don’t have to write console.log(userChoice())
I hope this helped you
Thank you so much for your help. However, I decided to go a different route after I had played with that for a few days and accidentally uploaded the old file to GIT. I have corrected this error, so, if you have a chance, please re-visit and checkout what I thought I had submitted! Thanks again!
Oh cool, I like it) Let me recommend just one more thing I find very useful. It’s about adding comments to your code, I find it a really valuable habit. As long as we quickly forget how our code functions, having comments in it makes it much easier to recall what you were doing when you have to get back to it. And it is even better for others to read it. I know, that at this moment most parts might seem too obvious to comment them, but this is just a good habit that is better acquired from the beginning.