javascript - 如何在我的 JavaScript 分支测验中显示我的路径?
问题描述
我看到了这个问题,并以此为基础进行了测验。你能告诉我如何打印用户遵循的路径吗?我想写更多的条件,例如,如果用户在路径 23 上行走,则向他展示这样的图片。
var questions = {
"text": "Why, besides the obvious are you buying wine today?",
"choices": [{
"label": "For a party",
"path": 1,
"question": {
"text": "For party...", //1 a
"choices": [{
"label": "It's a game day baby!",
"path": 11
},
{
"label": "It's Kelly's Bach party!",
"path": 12
},
]
}
},
{
"label": "As a gift",
"path": 2,
"question": {
"text": "How much do you wanna spend?", //1 b
"choices": [{
"label": "$",
"path": 21,
},
{
"label": "$$",
"path": 22,
"question": {
"text": "You sure? It this for...", //1 a
"choices": [{
"label": "Your yoga instructor Namaste",
"path": 221
},
{
"label": "A spouse/significant other",
"path": 222
},
{
"label": "Your boss / a client",
"path": 223
},
]
}
},
{
"label": "$$$",
"path": 23
},
]
}
},
]
};
解决方案
由于每个答案都有一个路径值,因此您可以参考最后选择的答案。所以在测验完成后:
alert("you have chosen the path:" + currentObj.choices[value].path );