我想创建一个决策树(如果名称正确),类似于下面的决策树(https://www.onefamily.com/lifetime-isa/,在“我是否有资格获得终身ISA?”下)
答案 0 :(得分:0)
您可以将物体与想要的问题,答案和评论联系起来。
function ask(question) {
if (!question) return;
var answer = prompt(question.question + '\n' + question.answers.map(({ answer }, i) => '\n' + i + ' ' + answer));
if (answer in question.answers) {
if (question.answers[answer].comment) {
alert(question.answers[answer].comment);
return;
}
ask(question.answers[answer]);
}
}
var tree = {
question: 'First of all, are you aged between 18 and 39 and a UK resident?',
answers: [
{
answer: 'Yes',
question: 'Great! Next, how are you planning to use your Lifetime ISA?',
answers: [
{
answer: 'I\'m buying my first (house)',
question: 'Is your first home going to cost less than £450,000?',
answers: [
{
answer: 'Yes',
question: 'And lastly, are you planning on buying your first home within the next five years?',
answers: [
{
answer: 'Yes',
comment: 'It looks like you could be eligible for a Lifetime ISA.'
},
{
answer: 'No',
comment: 'It looks like you could be eligible for a Lifetime ISA.'
}
]
},
{
answer: 'No',
comment: 'Based on the current government rules, Lifetime ISAs can be used to purchase homes costing £450,000 or less.'
}
]
},
{
answer: 'I\'m saving for my (...)',
question: 'What is your employment status?',
answers: [
{
answer: 'Employed',
comment: 'It looks like you could be eligible for a Lifetime ISA.'
},
{
answer: 'Self Employed',
comment: 'It looks like you could be eligible for a Lifetime ISA.'
},
{
answer: 'Not Employed',
comment: 'It looks like you could be eligible for a Lifetime ISA.'
}
]
}
]
},
{
answer: 'No',
comment: 'Our Lifetime ISAs are only available to UK residents aged between 18 and 39.'
}
]
},
question = tree;
ask(question);