0

HTML/JavaScript - Help with if, &&, else?

Okay so I made an attempt at something, but it was utterly bad so I deleted that section and only after doing that did I realise I could ask for help... Oops... So I'm trying to make a sort of Generator for my friend to help her with a little project, but it requires use of it, &&, else statements as well as grabbing randomly from lists within those... And my brain is melting OTL So... First off here's my first bit of basic generator (edited slightly just to have unimportant test words to generate) https://code.sololearn.com/Wnn1u330ZQfN/?ref=app Now, what I want to do now, is to somehow make it also print, on a separate line, something else depending on what was generated. So, let's say I get the result of "1" & "Red", I then want it to pick at random 'A', 'B', or 'C'. But if it gives "2 Red" I want randomly from 'Do', 'Re', or 'Mi' Etc etc

23rd Mar 2020, 12:01 PM
Kitsune Maika
Kitsune Maika - avatar
7 Réponses
+ 2
Kitsune Maika Did you see the deliberate mistake? 😉 The ifs need to check for "Red" and not "red". Code amended below. https://code.sololearn.com/WqgH7izlD4fk/?ref=app
24th Mar 2020, 7:13 AM
Russ
Russ - avatar
+ 1
Russ Thank you SO much! I think I got... Close-ish in my attempt I deleted before posting this question, but it was a lot more convoluted and longer and just... Not very good xD So thank you SO much! ❤️
24th Mar 2020, 7:26 AM
Kitsune Maika
Kitsune Maika - avatar
0
function newGen() { var randomNumber = Math.floor(Math.random() * (numbers.length)); var randomColor = Math.floor(Math.random() * (colors.length)); var number = numbers[randomNumber]; var color = colors[randomColor]; document.getElementById('outputDisplay').innerHTML = number + '&nbsp' + color; if (number === "1" && color === "red") { var list = ["Do", "Re", "Mi"]; } else if (number === "2" && color === "red") { var list = [...] } } Can't really see an easier way than this other than knowing what it is that decides the list and then trying to find a shortcut.
23rd Mar 2020, 3:41 PM
Russ
Russ - avatar
0
Russ that still doesn't display the new list? I edited my JavaScript to add your code and then tried rearranging the display to be after, and added the new var to the display but... Idk what's happening now... I did save it though so it can be looked at
23rd Mar 2020, 10:12 PM
Kitsune Maika
Kitsune Maika - avatar
0
You didn't copy it correctly. I saved the chosen number into a new variable, same with the chosen colour, then checked both of those in the if.
24th Mar 2020, 4:49 AM
Russ
Russ - avatar
0
Not a problem. Happy coding!
24th Mar 2020, 9:03 AM
Russ
Russ - avatar
0
Kitsune Maika Updated code to show another way you could do. It doesn't use "&&" - not sure if that was necessary - but, if that's ok, it should offer an easier way to create all the different lists. 👍🏻
24th Mar 2020, 10:09 AM
Russ
Russ - avatar