首页 > 解决方案 > 如何创建这样的决策树?

问题描述

我想创建一个决策树(如果这是正确的名称),如下所示(https://www.onefamily.com/lifetime-isa/,在“我有资格获得终身 ISA 吗?”)

决策树

标签: javascriptjqueryhtmlcss

解决方案


你可以拿一个带有想要的问题、答案和评论的对象。

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);


推荐阅读