Kaitlin Danson
Kaitlin Danson

Reputation: 113

is there a way to get info from a variable that is defined inside a function?

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!");
  }

Upvotes: 0

Views: 61

Answers (2)

andrewdcato
andrewdcato

Reputation: 104

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
}

Upvotes: 1

Potato Salad
Potato Salad

Reputation: 4650

Maybe this is what you need

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

Upvotes: 0

Related Questions