'Javascript: How can I determine if a user has asked the computer the correct series of questions within a specific group (A,B,C) of questions?
I'm sorry for the poorly phrased question, hopefully the below explanation of my program can help.
Summary of program:
- The user will ask the computer a question.
- The question will be matched using regular expressions.
- The computer will respond back to the user depending on if
/regular expression/.testis true or not
I have 3 groups of questions:
Group A (General Questions):
- What is your name?
- What is your age?
- What is your occupation?
- What is your height?
Group B (Occupation Questions):
- What is your occupation?
- What is your role in your occupation?
- Do you like your occupation?
- How long have you worked at your occupation?
- Why did you decide to work at your current job?
Group C (Family Life Questions):
- How many close family members do you have?
- Do you have any sisters or brothers?
- Do you have any kids?
- How is your family going?
- Where would you like to take a holiday with your family?
Let's consider two scenarios where the user asks questions:
Scenario 1:
- user asks "What is your name?" (Group A), "What is your age?" (Group A), "What is your occupation?" (Group A), "What is your height?" (Group A)
Scenario 2:
- user asks "What is your name?" (Group A), "What is your occupation?" (Group B), "Do you like your occupation?" (Group B), "What is your height?" (Group A), "Do you have any kids?" (Group B)
PROBLEM: I want to be able to determine if a user has asked all/most of the questions in one particular group before moving onto another, and not ask questions scattered across all 3 groups. Any ideas on how I could achieve this? I'm not sure if nested if statements could achieve this?
Here is a simple sample of my current code:
<html>
<form>
<input id="userInput" type="text" placeholder="Type your question here"></input>
<button type="submit">CLICK ME!</button>
</form>
<p id="computerResponse"></p>
</html>
JAVASCRIPT
<script>
var inputValue = document.getElementById("userInput").value;
var computerOutput = document.getElementById("computerResponse").innerHTML;
//Some Group A Questions
if (/what is your name?/ig.test(inputValue)) {
computerOutput = "My name is Harry.";
};
if (/what is your age?/ig.test(inputValue)) {
computerOutput = "I am 20 years old.";
};
if (/what is your height?/ig.test(inputValue)) {
computerOutput = "I am 20 years old.";
};
//Some Group B Questions
if (/do you like your occupation?/ig.test(inputValue)) {
computerOutput = "Yes, I do.";
};
if (/how long have you worked at your occupation?/ig.test(inputValue)) {
computerOutput = "I have worked here for 4 years.";
};
if (/why did you decide to work at your current job?/ig.test(inputValue)) {
computerOutput = "I am an accountant.";
};
//Some Group C Questions
if (/do you have any kids?/ig.test(inputValue)) {
computerOutput = "Yes, I have a 8 year old daughter.";
};
if (/do you have any sisters or brothers?/ig.test(inputValue)) {
computerOutput = "Yes, I have one brother.";
};
if (/How is your family going?/ig.test(inputValue)) {
computerOutput = "They are going great.";
};
</script>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
