Build a Quiz Game - Build a Quiz Game

Tell us what’s happening:

I can’t seem to get any user stories to pass, It even fails to acknowledge my array! Pls if u can help point out something wrong in the code, I’ve been here for hours.

The only feedback I’ve gotten is that the .length property is unreadable or something, but like .length are allowed in arrays so…

Your code so far

const questions =  [
  {category: "Colours", question:"Which is a primary colour?", choices: ["Purple", "Blue", "Orange"], answer: "Blue"},
  {category: "Shapes", question: "Which is not a polygon?", choices: ["Circle", "Square", "Triange"], answer: "Cirlce"},
  {category: "Numbers", question: "Which of the following is an odd number?", choices: ["12", "47", "106"], answer: "47"},
  {category: "Animals", question: "Which animals is canivorous?", choices: ["Giraffes", "Crocodiles", "Pigs"], answer: "Crocodiles"},
  {category: "Plants", question: "Which plant is a flower?", choices: ["Bromeliads", "Eggplant", "Marigold"], answer: "Marigold"}
];

// Random Question function below
function getRandomQuestion(questions) {
  let run = Math.round(Math.random() * questions.length);
  return run.question;
}

// To Choose a questions object at random
const randQuest = Math.round(Math.random() * questions.length);
// Choice of the random question object
const randCh = questions[randQuest.choices];


function getRandomComputerChoice(randCh) {
  return Math.round(Math.random() * randCh.length);
}

let compCh = getRandomComputerChoice(randCh);

function getResults(randQuest, compCh) {
  let ans = randQuest.answer;
  if(compCh === ans) {
    return "The computer's choice is correct!";
  } else {
    return `The computer's choice is wrong. The correct answer is: ${ans}`
  }
};

console.log(getRandomQuestion(questions));
console.log(getRandomComputerChoice(randCh));
console.log(getResults(randQuest, compCh));


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36

Challenge Information:

Build a Quiz Game - Build a Quiz Game

That sounds important. I would fix that before worrying about any tests.

What’s the exact error?

Deleting the console.log() lines and the
let compCh = getRandomComputerChoice(randCh);
line makes a lot failed test dissapear.

What remains is test 7-8-9.

I do some tests further.

1 Like

How did you wrote your code?

Have you tested your functions separately before writing the next one?
Haven you used console.log() to check if your const values are showing what you wanted to see?

2 Likes

Okay, I was able to rewrite your code to pass the challenge.

The main problem is simply not handling Objects correctly.

Just a few example:

let run = Math.round(Math.random() * questions.length);
  return run.question;

run will be a simply a number, not an object to acces it´s run.question data.

const randQuest = Math.round(Math.random() * questions.length);

let ans = randQuest.answer;

almost the same issue.

You probably wanted the data from the questions array instead.
Those errors and a few other together messes up the tests badly.

There is two ways to fix it:

  1. Rewrite your whole code and test everything you write while you coding.
  2. Try to review every code bit, wich try to acces an object. Wich is what I did and as you see, it´s time consuming. (+writing this post too of course)

It took me more than a month to get used to Objects, so I understand how hard it is.
With practice, you will learn that too, just use some simply tools:

  • You write a code:
const item = obj.key;
  • Than you check if it´s working:
const item = obj.key;
console.log(item);
  • if it not working, you may change the code:
const item = obj["name"];
console.log(item);
  • repeat until it works as you wanted.

Same with functions, but always before the return, otherwise it doesn´t runs:

function wordToUpperCase(word) {
  console.log(word.toUpperCase()) //will show
  return word.toUpperCase();
  console.log(word.toUpperCase()) //will not show, text turns grey colored indicating it will not run
}

console.log(wordToUpperCase("paPaYa"));

You can use comments too, to check every code separetly, if you wish to rewrite your current code. Otherwise you can´t use the console, because the errors hides the console.log() results.

// one-line comment; code will not run here

/*
  multiplie-line comment; nothing will run between /* and */
*/

Conclusion:
I suggest to hit the reset button and writing a new code, because you can practice the behaviour of self-debuggin while coding. Wich will be a useful skill on the long run.

Reviewing your current code is pure debugging, but if you don´t understand how to use Objects correctly, you will have a hard time guessing, how to do the debugging.

Don´t give up! We are here to further assist you, if you ask for it.

1 Like

is randCh here an array?

1 Like

Thanks man it’s worked

1 Like