Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How do I get this program to pick the question randomly and then keep picking new random questions?

I have this program and it picks a random question with a relative answer, however it doesn't pick another random question it just increments from where the random question was picked, how do I make it move onto another random question in the array? http://codepen.io/Viura/pen/mJZErK

4 Answers

One way to do it, could use some refactoring most likely...

http://codepen.io/ernestson/pen/rVEZMG/?editors=101

Zecare, take the "Object Oriented Javascript" class with Andrew C. The last section is a project "quiz" just like this, but at a higher level and with more features (constructor functions, object literals, score keeping, and progress tracking). I think it will help you learn how to write a better quiz!

So this is what I've come up with so far:

var questions = [
  ["Taxonomy is the __________ of organisms.","structure","classification","dissecting","opening","B"],
  ["How many charactersitics do all living things have in common?", "7","4","6","3","A"],
  ["Which of the following is not a taxanomic classifcation?", "kingdom", "height", "phyla","class","B"],
  ["Various species are located and classified in an ____________.", "habitat","chart","ecosystem","group","C"],
  ["The three main symbiotic relationships are: Mutalism, Paramatism and ____________.", "commensalism","existentialism","communism","pragmatism","A"]
];

var questionNumber = 1;
function randomNumber(){
  return Math.floor(Math.random() * questions.length);
}

function renderQuestion() {
  var test = document.getElementById("test");
  var num = randomNumber();
  test.innerHTML = "<div id='question'>"+questionNumber +") "+questions[num][0]+"</div>";
  test.innerHTML += "<button onclick='checkAnswer()' value='A'>"+"(a) "+questions[num][1]+"</button>"+"</br>";
  test.innerHTML += "<button onclick='checkAnswer()' value='B'>"+"(b) "+questions[num][2]+"</button>"+"</br>";
  test.innerHTML += "<button onclick='checkAnswer()' value='C'>"+"(c) "+questions[num][3]+"</button>"+"</br>";
  test.innerHTML += "<button onclick='checkAnswer()' value='D'>"+"(d) "+questions[num][4]+"</button>"+"</br>";
  questionNumber ++;
}

renderQuestion();

function checkAnswer(value) {
  renderQuestion();
}

I'm out of time to figure out a way to actually check the answer so I leave that to you. But for now, this will run over and over again.

Thank you, for trying to help me but this doesn't help, I already know how to render the questions and answer randomly, What I am trying to achieve is when I click on the answer, a next random question with relative answers is suppose to be rendered from the array it's suppose to increment randomly from the array.

Zecare, I do not see your codepen. Did the answer above satisfy your requirements?

http://codepen.io/Viura/pen/bdPLyx No I still can't figure out how to render a random question from the array when the button is clicked. I want to render 3 questions randomly from the array but one at a time and when the answer is clicked it moves on to a next random question, like I have everything else sorted out I just can't figure this out.

can you just paste your code here? your codepen is gone again. i'll be able to work on this later today.

Hey I got it figured out I am currently on the Ajax course, when I am finish I will do that one next, thanks a lot for the advice.

sure, good luck! let me know if you have more questions! I'm a newbie, but i'll do my best!