0

I am trying to figure out if there is a way to access the information stored inside a variable that I defined inside a function? I am kinda confused on how to do what I am trying to do here...

note: this isn't the full code, but the piece of the code I need help with.

let question1 = new Question("What is California State Flower?", "1. Rose. 2. Tulip. 3. Poppy");
firstQuestion();


function firstQuestion(){
  let someAnswer = prompt(question1.questionName + " " + question1.questionString);
}

if (someAnswer == "poppy"){

I am trying to use the if statement to figure out if a question answer is correct, but I can't do that because someAnswer was defined inside the function.... and i'm not sure if there is a way to do this without using a function?

Update:

Ok, I got that piece working, but now my code's if/else statement isn't working. if i put in the wrong answer, it says I have the right answer. I don't really see any logical reason for that...

//store score total

let pointsCount = 0;

//questions
class Question {
questionName: string;
questionString: string;

constructor(questionName:string, questionString:string){
  this.questionName = questionName;
  this.questionString = questionString;
}
}


//question one
let question1 = new Question("What is the California State Flower?", "1. Rose. 2. Tulip. 3. Poppy.");
let firstAnswer = firstQuestion();
function firstQuestion(){
  return prompt(question1.questionName + " " + question1.questionString);
}

if (firstAnswer === "Poppy" || "poppy"){
  pointsCount ++;
alert("You got it!" + " " + "You now have" + " " + pointsCount + " " + "points!");

} else {
  alert("Wrong!" + " " + "You now have" + " " + pointsCount + " " + "points!");

}


//question two

let question2 = new Question("What is the California State Bird?","1. Quail. 2. Eagle. 3. Penguin.")

let secondAnswer = secondQuestion();

function secondQuestion(){
  return prompt(question2.questionName + " " + question2.questionString);
}

if (secondAnswer === "quail" || "Quail"){
  pointsCount++;
  alert("You got it!" + " " + "You now have" + " " + pointsCount + " " + "points!");
} else if (secondAnswer !== "quail" || "Quail") {
    alert("Wrong!" + " " + "You now have" + " " + pointsCount + " " + "points!");
  }
Kaitlin Danson
  • 113
  • 1
  • 13

2 Answers2

1

You're close; you're not returning anything from your firstQuestion function, so nothing's ever really going to happen when you run this.

let question1 = new Question("What is California State Flower?", "1. Rose. 2. Tulip. 3. Poppy");
let answer = firstQuestion();


function firstQuestion(){
  //  return whatever the user enters in the prompt
  return prompt(question1.questionName + " " + question1.questionString);
}

if (answer.toLowerCase() == "poppy"){
  // call .toLowerCase on your answer to ensure you've covered capitalization edge-cases
}
andrewdcato
  • 94
  • 2
  • 8
0

Maybe this is what you need

let someAnswer;
function firstQuestion(){
  someAnswer = prompt(question1.questionName + " " + question1.questionString);
}
Abana Clara
  • 4,602
  • 3
  • 18
  • 31
  • 1
    A side note: in this particular case there is really no reason to prefer free variables over simple `return` from a function. It does not bring any benefits and makes code harder to maintain. – zerkms Mar 10 '17 at 05:41
  • 2
    there isn't even a point of having the function as prompt is not an asynchronous action. `let someAnswer = prompt('question')` – synthet1c Mar 10 '17 at 05:52
  • He could either remove the code from the entire function itself or do a return, depending on what OP needs – Abana Clara Mar 10 '17 at 06:05
  • I feel like its easier having it as a function? Just because I am going to have to have multiple questions? or am I wrong? – Kaitlin Danson Mar 10 '17 at 06:53
  • Yes so you do a return for your function so every time the function is called, a value is returned. – Abana Clara Mar 10 '17 at 06:53