Skip to content

Commit

Permalink
create global variable & store index of each correct answer, within c…
Browse files Browse the repository at this point in the history
…reateQUestion fxn
  • Loading branch information
rbozek committed Jan 3, 2024
1 parent eabcef2 commit 70820dc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
26 changes: 21 additions & 5 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ const questionsCat1 = Object.values(questionsImportCat1);


/*------------ Variables ------------*/
let playerScore // use in init or render
let playerScore // can reset in init or render
let currentQuesIdx = 0
let currQuesCorrAnsIdx // for playerChooseAnswer



/*---- Cached Element References ----*/
Expand All @@ -25,6 +27,7 @@ btnReset.addEventListener('click', reset)


/*------------ Functions ------------*/

function renderQuestionCat1() {
//HOLDS ONE SPECIFIC QUESTION:
let singleQuestion = questionsCat1[currentQuesIdx]
Expand All @@ -35,9 +38,19 @@ function renderQuestionCat1() {
// console.log(answersToSingleQuestion);

//loop through quesA's
answersToSingleQuestion.forEach((eachA) => {
answersToSingleQuestion.forEach((eachA, index) => {
let renderedAnswer = document.createElement('li')
renderedAnswer.textContent = `${eachA.answer}`

// similar solution to earlier this morning: likely not using
// renderedAnswer.id = eachA.correctAnswer

if (eachA.correctAnswer == true) { // to log answer index for current question
console.log(eachA.correctAnswer);
currQuesCorrAnsIdx = index; // ^^ global!!!
console.log(currQuesCorrAnsIdx);
}

//can take advantage of bubbling by adding to container, but keeping here for now:
renderedAnswer.addEventListener('click', playerChooseAnswer)
answers.appendChild(renderedAnswer)
Expand All @@ -46,17 +59,20 @@ function renderQuestionCat1() {
}

function playerChooseAnswer(evt){

// console.log(evt.target.textContent);

// if (){
// }

console.log(evt.target);
console.log(currQuesCorrAnsIdx);

currentQuesIdx += 1
if (currentQuesIdx >= questionsCat1.length){
gameOverTest()
}
question.textContent = '' // this line isnt needed?
answers.innerHTML = ''
console.log(currentQuesIdx);
// console.log(currentQuesIdx);
console.log('player choice click works');
renderQuestionCat1()
}
Expand Down
5 changes: 1 addition & 4 deletions js/pseudocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
// // get LI's clickable / - event listener for those


1 - reset button issue
2 -

// function for chooseAnswer - that's where we'll use conditional to see if it matches "true" from quesAs?
// finish function for chooseAnswer - that's where we'll use conditional to see if it matches "true" from quesAs?
// update playerScore - compare playerChoice with correctAnswer, if === then add points, else then nothing
// & display playerScore
// end-game scenario when a Category is finished!
Expand Down
10 changes: 5 additions & 5 deletions js/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ const questionsImportCat1 = [
{
quesQ: "What do you do question 1 etc etc?",
quesAs: [
{ answer: 'be rude', correctAnswer: false },
{ answer: 'be nice', correctAnswer: true },
{ answer: 'be rude', correctAnswer: false },
{ answer: 'be crude', correctAnswer: false },
{ answer: 'be tude', correctAnswer: false },
]
},
{
quesQ: "Question 2, what's good to do??",
quesAs: [
{ answer: 'help people', correctAnswer: true },
{ answer: 'hurt people', correctAnswer: false },
{ answer: 'help people', correctAnswer: false },
{ answer: 'hurt people', correctAnswer: true },
{ answer: 'kill people', correctAnswer: false },
{ answer: 'steal someones car', correctAnswer: false },
]
Expand All @@ -22,8 +22,8 @@ const questionsImportCat1 = [
quesAs: [
{ answer: 'bad friend', correctAnswer: false },
{ answer: 'no friend', correctAnswer: false },
{ answer: 'donut stealer', correctAnswer: false },
{ answer: 'Good friend', correctAnswer: true },
{ answer: 'donut stealer', correctAnswer: true },
{ answer: 'Good friend', correctAnswer: false },
]
},
]
Expand Down

0 comments on commit 70820dc

Please sign in to comment.