+ 1
28th Oct 2017, 4:59 PM
bobbie
bobbie - avatar
15 Answers
+ 8
Yes, if you're beginner, start with two parrallel arrays, it's the easiest to implement... and when you will be more skilled, you could once object (basically a list holding 2 sublists) container for questions and answers stored together (next step will be to use custom objects, as container of both lists or as container for a couple of question-answer, this one stored in a list)... Parrallel array solution: var questions = ['q0','q1','q2','q3']; var answers = ['a0','a1','a2','a3']; // assume that there's same count of item in both arrays var index = Math.floor(Math.random()*questions.length); prompt(questions[index]); alert(answer[index]); Unique (container) array solution: var quizz = [['q0','q1','q2','q3'],['a0','a1','a2','a3']]; // assume that there's same count of item in both sub-arrays var index = Math.floor(Math.random()*questions.length); prompt(quizz[0][index]); alert(quizz[1][index]); Object holding arrays solution: var quizz = {questions:['q0','q1','q2','q3'],answers:['a0','a1','a2','a3']}; var index = Math.floor(Math.random()*quizz.questions.length); prompt(quizz.questions[index]); alert(quizz.answers[index]); Unique array holding question/answer pairs custom objects: var quizz = [{ask:'q0',ans:'a0'},{ask:'q1',ans:'a1'},{ask:'q2',ans:'a2'},{ask:'q3',ans:'a3'}]; var index = Math.floor(Math.random()*quizz.length); prompt(quizz[index].ask); alert(quizz[index].ans); For the last one, you will be probably advised to use custom object constructor to much more readable code than litteral assignement: function QuestionAnswer(question,answer) { this.ask = question; this.ans = answer; } var quizz = []; quizz.append('q0','a0'); quizz.append('q1','a1'); quizz.append('q2','a2'); quizz.append('q3','a3'); var index = Math.floor(Math.random()*quizz.length); prompt(quizz[index].ask); alert(quizz[index].ans);
29th Oct 2017, 4:55 AM
visph
visph - avatar
+ 6
I believe that's not the first time you've qualified with heartiness my answer as very useful, but it seems to me that you never attribute the best answer mark for anybody in any of your question thread: that's not quite encouraging ^^ (at least you answer, thanks and upvote, but I need more encouragements to compensate for a lot of other unthanked answers of mine by more less respectuous members: honnestly, I have free time actually to help here, but since few months ago I just went sometimes -- and even that have incited me in most part to decline the offer to be moderator -- because of majority of sololearner attitude -- focussing on XPs and chalenge, so not really supporting an helper like me who don't play chalenges and no more social "game" to be enough visible from others members which almost blindly upvote those who are upvoting/following them: I follow only a minimal number of sololearners because I'm being annoying by the messy of main notifications, even with challenge-related ones desactivated, mainly just reading the Q&A discussion list, and answering mostly for technical questions, passing over less or more "social" thread unrelated to coding :P)
29th Oct 2017, 6:34 AM
visph
visph - avatar
+ 6
Hey visph sorry you are feeling unappreciated I do value your advice and Im certain others do as well. People like yourself make learning here a great experience being so helpful. It's easy to become discouraged when we new learners hit roadblocks in our codes people like you helping makes a huge impact. I only recently found out how to choose the correct answer I will go back to previous questions and mark them as well. Thanks again and I hope you will continue encouraging and helping others.
29th Oct 2017, 3:25 PM
bobbie
bobbie - avatar
+ 6
Javascript: var index; // declare 'index' as global var to be accessible from both functions function newAnswer(){ document.getElementById('answerDisplay').innerHTML=answer[index]; } function newRiddle(){ index=Math.floor(Math.random()*(20)); // remove 'var' keyword to use the global var document.getElementById('riddleDisplay').innerHTML=riddle[index]; } Html: You have many wrong things in your html, even if all are almost corrected by the html browsers/viewers... + <body></body> must appear only once in a document, embending all the visual elements of the page + 'bgcolor' attribute is deprecated, you should use 'background-color' (or 'background' in shorthand) css property (in the 'style' attribute, if you doesn't still use external css rules ;)) + after your <h1></h1>, you open a <div> and next a <span> which you never close (I'm sure for the <span>, I guess for the <div>: maybe it's another one later in code wich you doesn't close ^^) + don't use the <span> in this case, if you just put it for declare the css rule in its 'style' attribute... rather put this rule in the 'style' property of the <body>, as you want apply the font-size on the whole document? + incidentally, the <script></script> tag isn't necessary in code playground, it's required only in "real" situation, to link an external js file or to embed the js code inside...
29th Oct 2017, 6:42 PM
visph
visph - avatar
+ 4
In the same way you got an ask button to work in your code ^^
29th Oct 2017, 3:17 AM
visph
visph - avatar
+ 4
You need to handle two (index related) list (questions and answers), even in once object... Get a random question index, and you will have the same index to access to your second list (even if embeded in only once list) ^^
29th Oct 2017, 4:22 AM
visph
visph - avatar
+ 4
Thanks visph!!! Very informed answer!!! I did come across a few things about a this method just not enough to implement it. I will work on trying one or more of your suggestions tomorrow. Thanks again!!! 🙂
29th Oct 2017, 5:55 AM
bobbie
bobbie - avatar
+ 4
Thanks visph the parallel method is working great!!!
29th Oct 2017, 4:33 PM
bobbie
bobbie - avatar
+ 4
Thanks again visph the new info works like a charm you are awesome!!!
29th Oct 2017, 7:44 PM
bobbie
bobbie - avatar
+ 3
Thanks Visph!!! Like a parallel array? Been researching arrays for days now first I found of such lol. Thanks for the index related clue!!! I will try it.
29th Oct 2017, 4:33 AM
bobbie
bobbie - avatar
+ 3
I agree Gustav awesome information provided by visph!!! Those who take time to help us with answers here are amazing!!!
29th Oct 2017, 7:32 PM
bobbie
bobbie - avatar
+ 2
Thought the answers needed to be in the question array ? or how will it line up with the random questions? tried a few methods no luck yet.
29th Oct 2017, 4:15 AM
bobbie
bobbie - avatar
+ 2
visph, that's some great code you pushed there.
29th Oct 2017, 5:03 PM
Kustaa
+ 2
Hey visph not to be a bother lol trying to combine the 1st and 2nd codes to get questions and answers to appear on document only after clicks. This is as close as I have gotten. When I separated them I got random answers with questions. Lol Any clue how to add the onclick here? I would think I have tried it all but certain I missed something . https://code.sololearn.com/W8WgR9MBy9fe/?ref=app
29th Oct 2017, 5:32 PM
bobbie
bobbie - avatar
+ 1
bobbie, you just had web development class in this thread ☺👍
29th Oct 2017, 7:13 PM
Kustaa