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!");
  }
3
  • define variable someAnswer outside function Commented Mar 10, 2017 at 5:37
  • 1
    return the result from the function. Commented Mar 10, 2017 at 5:38
  • Check about scope in this Q&A: stackoverflow.com/questions/500431/… Commented Mar 10, 2017 at 5:41

2 Answers 2

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

hmmm...I tried this and..now my if/else statement isnt working? if i answer the wrong question, it says I got the right answer.... this doesnt even make any sense to why that would happen lol
nvm I got it haha.
0

Maybe this is what you need

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

5 Comments

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.
there isn't even a point of having the function as prompt is not an asynchronous action. let someAnswer = prompt('question')
He could either remove the code from the entire function itself or do a return, depending on what OP needs
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?
Yes so you do a return for your function so every time the function is called, a value is returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.