Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >获取可能的问答路径(JavaScript)

获取可能的问答路径(JavaScript)
EN

Stack Overflow用户
提问于 2021-06-18 07:26:50
回答 2查看 117关注 0票数 1

我在寻找基于问题选项的所有可能的问题答案路径,我一整天都在绞尽脑汁,似乎无法理解为什么我的代码不起作用。

测试代码:

代码语言:javascript
运行
AI代码解释
复制
const originalQuestions = {
    1: {
        title: "Title",
        firstQuestion: true,
        options: [{
            tooltip: "",
            nextQuestion: 2
        }, {
            tooltip: "",
            nextQuestion: 2
        }, {
            tooltip: "",
            nextQuestion: 10000
        }]
    },
    2: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 3
        }, {
            tooltip: "",
            nextQuestion: 3
        }, {
            tooltip: "",
            nextQuestion: 3
        }, {
            tooltip: "",
            nextQuestion: 3
        }]
    },
    3: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 4
        }, {
            tooltip: "",
            nextQuestion: 4
        }, {
            tooltip: "",
            nextQuestion: 4
        }]
    },
    4: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 13
        }, {
            tooltip: "",
            nextQuestion: 5
        }]
    },
    5: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 6
        }, {
            tooltip: "",
            nextQuestion: 6
        }, {
            tooltip: "",
            nextQuestion: 6
        }, {
            tooltip: "",
            nextQuestion: 10000
        }]
    },
    6: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 7
        }, {
            tooltip: "",
            nextQuestion: 7
        }, {
            tooltip: "",
            nextQuestion: 7
        }, {
            tooltip: "",
            nextQuestion: 7
        }, {
            tooltip: "",
            nextQuestion: 14
        }]
    },
    7: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 17
        }, {
            tooltip: "",
            nextQuestion: 17
        }, {
            tooltip: "",
            nextQuestion: 17
        }, {
            tooltip: "",
            nextQuestion: 17
        }, {
            tooltip: "",
            nextQuestion: 17
        }]
    },
    8: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 9
        }, {
            tooltip: "",
            nextQuestion: 9
        }, {
            tooltip: "",
            nextQuestion: 9
        }]
    },
    9: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 10
        }, {
            tooltip: "",
            nextQuestion: 10
        }, {
            tooltip: "",
            nextQuestion: 10
        }]
    },
    10: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 11
        }, {
            value: "Roof",
            attribute: "Flue Exit",
            tooltip: "",
            nextQuestion: 15
        }]
    },
    11: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 12
        }, {
            tooltip: "",
            nextQuestion: 12
        }]
    },
    12: {
        finalQuestion: true,
        input: true,
        placeHolder: 'e.g SWS'
    },
    13: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 6
        }, {
            tooltip: "",
            nextQuestion: 6
        }]
    },
    14: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 7
        }, {
            tooltip: "",
            nextQuestion: 7
        }, {
            tooltip: "",
            nextQuestion: 10000
        }]
    },
    15: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 12
        }, {
            tooltip: "",
            nextQuestion: 12
        }]
    },
    17: {
        title: "Title",
        options: [{
            tooltip: "",
            nextQuestion: 8
        }, {
            tooltip: "",
            nextQuestion: 8
        }, {
            tooltip: "",
            nextQuestion: 8
        }, {
            tooltip: "",
            nextQuestion: 8
        }, {
            tooltip: "",
            nextQuestion: 8
        }]
    },
    // Errors
    10000: {
        isError: true,
        title: "Finally, what is the first part of your postcode?",
        error: "Postcode"
    }
};

function loopPaths(currentArrayPath, questionNum, currentQuestion, paths) {

    if (questionNum > 5) {
   return false;
  }

    if (typeof currentQuestion.finalQuestion != 'undefined') {
    return false;
  } else {
    const question = currentQuestion.options;  
    const validPaths = question.filter((e) => e.nextQuestion !== 10000);   
    const clonedPath = [...paths[currentArrayPath][0]];
            
    for (var i = 0; i < validPaths.length; i++) {
      const e = validPaths[i];
            
      if (typeof paths[currentArrayPath][i] == 'undefined') {
        paths[currentArrayPath][i] = [...clonedPath];
      }
      
      paths[currentArrayPath][i].push(questionNum);
      
      loopPaths(currentArrayPath, e.nextQuestion, originalQuestions[e.nextQuestion], paths);
    }
  }
}

function possiblePaths() {
    const question1 = originalQuestions[1].options;
    const validPaths = question1.filter((e) => e.nextQuestion !== 10000);
  
  let paths = [];
  
  /*validPaths.forEach((e, i) => {
    paths[i] = [[1]];
  });*/
      
  /* 
  for (var i = 0; i < validPaths.length; i++) {
    const e = validPaths[i];
           
    loopPaths(e.nextQuestion, boilerQuestions[e.nextQuestion], paths);
  }   */
  
  // Testing with first question, first option
  paths[0] = [[1]];
  loopPaths(0, 2, originalQuestions[2], paths);
  
  console.log(paths);
}

possiblePaths();

似乎正在发生的事情:(它没有建立正确的路径,也没有构建不同的可能性)

代码语言:javascript
运行
AI代码解释
复制
[
  [
    [
      1,
      2,
      3,
      4,
      5,
      4,
      5,
      4,
      5,
      3,
      4,
      5,
      4,
      5,
      4,
      5,
      3,
      4,
      5,
      4,
      5,
      4,
      5,
      3,
      4,
      5,
      4,
      5,
      4,
      5
    ],
    [
      1,
      2,
      3,
      4,
      5,
      3,
      4,
      5,
      4,
      5,
      2,
      4,
      5,
      3,
      4,
      5,
      4,
      5,
      4,
      5,
      3,
      4,
      5,
      4,
      5,
      4,
      5,
      3,
      4,
      5,
      4,
      5
    ]
  ]
]

应该展示的例子:(我想知道是否有一种更简单的方法来构建从1 ->到最后一个问题(12)的不同可能性)

代码语言:javascript
运行
AI代码解释
复制
[
  [
    [
      1,
      2,
      3,
      4,
      13,
      7,
      17,
      8,
      9,
      10,
      11,
      12
    ],
    [
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      ....
    ]
  ]
]

它将如何工作:

用户点击问题1 ->按压选项1 ->用户点击问题2 ->页面选项2 ->用户点击问题3 ->页面选项1 ->用户点击问题4 ->用户点击问题1 ->用户点击问题13

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-18 08:30:39

您可以使用递归生成器函数。

代码语言:javascript
运行
AI代码解释
复制
const originalQuestions = { 1: { title: "Title", firstQuestion: true, options: [{ tooltip: "", nextQuestion: 2 }, { tooltip: "", nextQuestion: 2 }, { tooltip: "", nextQuestion: 10000 }] }, 2: { title: "Title", options: [{ tooltip: "", nextQuestion: 3 }, { tooltip: "", nextQuestion: 3 }, { tooltip: "", nextQuestion: 3 }, { tooltip: "", nextQuestion: 3 }] }, 3: { title: "Title", options: [{ tooltip: "", nextQuestion: 4 }, { tooltip: "", nextQuestion: 4 }, { tooltip: "", nextQuestion: 4 }] }, 4: { title: "Title", options: [{ tooltip: "", nextQuestion: 13 }, { tooltip: "", nextQuestion: 5 }] }, 5: { title: "Title", options: [{ tooltip: "", nextQuestion: 6 }, { tooltip: "", nextQuestion: 6 }, { tooltip: "", nextQuestion: 6 }, { tooltip: "", nextQuestion: 10000 }] }, 6: { title: "Title", options: [{ tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 14 }] }, 7: { title: "Title", options: [{ tooltip: "", nextQuestion: 17 }, { tooltip: "", nextQuestion: 17 }, { tooltip: "", nextQuestion: 17 }, { tooltip: "", nextQuestion: 17 }, { tooltip: "", nextQuestion: 17 }] }, 8: { title: "Title", options: [{ tooltip: "", nextQuestion: 9 }, { tooltip: "", nextQuestion: 9 }, { tooltip: "", nextQuestion: 9 }] }, 9: { title: "Title", options: [{ tooltip: "", nextQuestion: 10 }, { tooltip: "", nextQuestion: 10 }, { tooltip: "", nextQuestion: 10 }] }, 10: { title: "Title", options: [{ tooltip: "", nextQuestion: 11 }, { value: "Roof", attribute: "Flue Exit", tooltip: "", nextQuestion: 15 }] }, 11: { title: "Title", options: [{ tooltip: "", nextQuestion: 12 }, { tooltip: "", nextQuestion: 12 }] }, 12: { finalQuestion: true, input: true, placeHolder: 'e.g SWS' }, 13: { title: "Title", options: [{ tooltip: "", nextQuestion: 6 }, { tooltip: "", nextQuestion: 6 }] }, 14: { title: "Title", options: [{ tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 10000 }] }, 15: { title: "Title", options: [{ tooltip: "", nextQuestion: 12 }, { tooltip: "", nextQuestion: 12 }] }, 17: { title: "Title", options: [{ tooltip: "", nextQuestion: 8 }, { tooltip: "", nextQuestion: 8 }, { tooltip: "", nextQuestion: 8 }, { tooltip: "", nextQuestion: 8 }, { tooltip: "", nextQuestion: 8 }] }, 10000: { isError: true, title: "Finally, what is the first part of your postcode?", error: "Postcode" } };

function* fnName(key, result = [key]) {
    let options = originalQuestions[key]?.options;
    if (!options) yield result
    else for (const id of new Set(options.map(v => v.nextQuestion)))
        yield* fnName(id, result.concat(id));
}
console.log("Prettify", Array.from(fnName(1), path => path.join(", ")))

我简单地通过第一个问题id (fnName(1)),在这个例子中它的编号是1,因为这是第一个问题。(originalQuestions[1].firstQuestion = true)

票数 4
EN

Stack Overflow用户

发布于 2021-06-18 09:38:33

努尔最棒的答案就是我会怎么解决这个问题。我可能会建议做一些改变,这样就有足够的不同,可以单独发布一个帖子-

代码语言:javascript
运行
AI代码解释
复制
function* paths(t, key) {
  const { options } = t[key] ?? {}
  if (options == null) return yield [key]
  for (const id of new Set(options.map(v => v.nextQuestion)))
    for (const path of paths(t, id))
      yield [key, ...path]
}

const originalQuestions =
  { 1: { title: "Title", firstQuestion: true, options: [{ tooltip: "", nextQuestion: 2 }, { tooltip: "", nextQuestion: 2 }, { tooltip: "", nextQuestion: 10000 }] }, 2: { title: "Title", options: [{ tooltip: "", nextQuestion: 3 }, { tooltip: "", nextQuestion: 3 }, { tooltip: "", nextQuestion: 3 }, { tooltip: "", nextQuestion: 3 }] }, 3: { title: "Title", options: [{ tooltip: "", nextQuestion: 4 }, { tooltip: "", nextQuestion: 4 }, { tooltip: "", nextQuestion: 4 }] }, 4: { title: "Title", options: [{ tooltip: "", nextQuestion: 13 }, { tooltip: "", nextQuestion: 5 }] }, 5: { title: "Title", options: [{ tooltip: "", nextQuestion: 6 }, { tooltip: "", nextQuestion: 6 }, { tooltip: "", nextQuestion: 6 }, { tooltip: "", nextQuestion: 10000 }] }, 6: { title: "Title", options: [{ tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 14 }] }, 7: { title: "Title", options: [{ tooltip: "", nextQuestion: 17 }, { tooltip: "", nextQuestion: 17 }, { tooltip: "", nextQuestion: 17 }, { tooltip: "", nextQuestion: 17 }, { tooltip: "", nextQuestion: 17 }] }, 8: { title: "Title", options: [{ tooltip: "", nextQuestion: 9 }, { tooltip: "", nextQuestion: 9 }, { tooltip: "", nextQuestion: 9 }] }, 9: { title: "Title", options: [{ tooltip: "", nextQuestion: 10 }, { tooltip: "", nextQuestion: 10 }, { tooltip: "", nextQuestion: 10 }] }, 10: { title: "Title", options: [{ tooltip: "", nextQuestion: 11 }, { value: "Roof", attribute: "Flue Exit", tooltip: "", nextQuestion: 15 }] }, 11: { title: "Title", options: [{ tooltip: "", nextQuestion: 12 }, { tooltip: "", nextQuestion: 12 }] }, 12: { finalQuestion: true, input: true, placeHolder: 'e.g SWS' }, 13: { title: "Title", options: [{ tooltip: "", nextQuestion: 6 }, { tooltip: "", nextQuestion: 6 }] }, 14: { title: "Title", options: [{ tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 7 }, { tooltip: "", nextQuestion: 10000 }] }, 15: { title: "Title", options: [{ tooltip: "", nextQuestion: 12 }, { tooltip: "", nextQuestion: 12 }] }, 17: { title: "Title", options: [{ tooltip: "", nextQuestion: 8 }, { tooltip: "", nextQuestion: 8 }, { tooltip: "", nextQuestion: 8 }, { tooltip: "", nextQuestion: 8 }, { tooltip: "", nextQuestion: 8 }] }, 10000: { isError: true, title: "Finally, what is the first part of your postcode?", error: "Postcode" } }

for (const path of paths(originalQuestions, 1))
  console.log(path.join(" -> "))
代码语言:javascript
运行
AI代码解释
复制
.as-console-wrapper { min-height: 100%; }

代码语言:javascript
运行
AI代码解释
复制
1 -> 2 -> 3 -> 4 -> 13 -> 6 -> 7 -> 17 -> 8 -> 9 -> 10 -> 11 -> 12
1 -> 2 -> 3 -> 4 -> 13 -> 6 -> 7 -> 17 -> 8 -> 9 -> 10 -> 15 -> 12
1 -> 2 -> 3 -> 4 -> 13 -> 6 -> 14 -> 7 -> 17 -> 8 -> 9 -> 10 -> 11 -> 12
1 -> 2 -> 3 -> 4 -> 13 -> 6 -> 14 -> 7 -> 17 -> 8 -> 9 -> 10 -> 15 -> 12
1 -> 2 -> 3 -> 4 -> 13 -> 6 -> 14 -> 10000
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 17 -> 8 -> 9 -> 10 -> 11 -> 12
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 17 -> 8 -> 9 -> 10 -> 15 -> 12
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 14 -> 7 -> 17 -> 8 -> 9 -> 10 -> 11 -> 12
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 14 -> 7 -> 17 -> 8 -> 9 -> 10 -> 15 -> 12
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 14 -> 10000
1 -> 2 -> 3 -> 4 -> 5 -> 10000
1 -> 10000
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68037804

复制
相关文章
JavaScript获取路径
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117720.html原文链接:https://javaforall.cn
全栈程序员站长
2022/07/05
1.4K0
JavaScript获取路径
JavaScript获取当前url路径
1、假设当前页完整地址是:https://www.qmblog.cn:8080/Home/Index?id=2&age=18 //获取当前窗口的Url var url = window.locatio
青梅煮码
2023/01/16
1.8K0
LeetCode - 所有可能的路径
我又重新开始更新LeetCode了,以后工作日更新LeetCode,周末更新东野圭吾的小说
晓痴
2019/07/24
7700
LeetCode - 所有可能的路径
LeetCode:所有可能的路径_797
给你一个有 n 个节点的 有向无环图(DAG),请你找出所有从节点 0 到节点 n-1 的路径并输出(不要求按特定顺序)
Yuyy
2022/06/28
3590
LeetCode:所有可能的路径_797
LeetCode-797-所有可能的路径
题目来自于力扣https://leetcode-cn.com/problems/all-paths-from-source-to-target
benym
2022/07/14
4440
Java文件路径/服务器路径的获取
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/157583.html原文链接:https://javaforall.cn
全栈程序员站长
2022/09/14
4.5K0
LeetCode 797. 所有可能的路径(DFS)
给一个有 n 个结点的有向无环图,找到所有从 0 到 n-1 的路径并输出(不要求按顺序)
Michael阿明
2020/07/13
7620
LeetCode 797. 所有可能的路径(DFS)
java获取classpath以外的路径
最近在使用以前写过的代码生成器(从表名可生成所有的代码)的时候,发现生成的文件都在classpath目录下,所有的文件都得自己拷到工程目录下,于是,想优化一下,取得classpath目录以外的路径,很简单,使用getCanonicalPath,如下
甲蛙全栈
2020/11/24
1.5K0
js 获取多级路径
数据结构  let treeData = [{ id: 1, label: '一级 1', children: [{ id: 4, label: '二级 1-1', children: [{ id: 9, label: '三级 1-1-1' }, { id: 10, label: '三级 1-1-2' }]
tianyawhl
2022/09/28
33.9K0
Golang
记录一下,方便下次使用: const dataFile = "../conf/db.yml" skip是要提升的堆栈帧数,0-当前函数,1-上一层函数,.... _, filename, _, _ := runtime.Caller(1) datapath := path.Join(path.Dir(filename), dataFile) golog.Info("================="+datapath+"------------------------------------------"
时光_赌徒
2020/05/26
5.1K0
python获取当前目录路径和上级路径
在使用python的时候总会遇到路径切换的使用情况,如想从文件夹test下的test.py调用data文件夹下的data.txt文件:
py3study
2020/01/08
9.3K0
java无法获取服务器上路径,JAVA获取服务器路径的步骤
ServletContext sc = (ServletContext)FacesContext.
全栈程序员站长
2022/09/15
1.9K0
根据对象路径获取对象的value
// 获取value的方法 obj为要获取的对象,path是路径 用.链接 var getPropByPath = function (obj, path) { let tempObj = obj path = path.replace(/\[(\w+)\]/g, '.$1') path = path.replace(/^\./, '') let keyArr = path.split('.') let i = 0 for (let
Java架构师必看
2021/08/23
3K0
java获取服务器路径_JAVA获取服务器路径的方法「建议收藏」
request.getSession().getServletContext().getRealPath(request.getRequestURI())
全栈程序员站长
2022/09/14
2.9K0
获取路径文件的后缀名字
使用QFileInfo获取路径文件的名字与后缀 测试文件 "/tmp/file.tar.gz" 1 获取文件名 返回不带名字的 file QString QFileInfo::baseName() const 返回名字和后缀 file.tar.gz QString QFileInfo::fileName() const 2 获取文件后缀 返回 "gz" QString QFileInfo::suffix() const 返回 "tar.gz" QString QFileInfo::completeSuffi
Qt君
2019/07/16
3.7K0
Python 获取当前路径的方法
模块搜索路径的字符串列表。由环境变量PYTHONPATH初始化得到。 sys.path[0]是调用Python解释器的当前脚本所在的目录。
AnRFDev
2021/02/01
2.2K0
你可能错过的现代 JavaScript 特性 [每日前端夜话0xE0]
尽管我在过去 7 年中几乎每天都在写 JavaScript 代码,但不得不承认,我实际上并不是很注意 ES 语言的发布声明。async/await 和 Proxies 之类的主要特性是一回事,但是每年都有稳定的小规模、渐进式的改进在不断涌现,因为总有一些东西需要学习。
疯狂的技术宅
2019/11/14
4870
JavaScript获取cookie的方法
之前都是使用 php 对 cookie 进行操作,今天有个需求,需要用 js 获取网站的 cookie 。下面开始:
德顺
2020/09/22
4.5K0
JavaScript获取cookie的方法
之前都是使用 php 对 cookie 进行操作,今天有个需求,需要用 js 获取网站的 cookie 。下面开始:
德顺
2023/08/25
5970
input file获取文件路径
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/160948.html原文链接:https://javaforall.cn
全栈程序员站长
2022/09/16
6.9K0

相似问题

Javascript问答的麻烦

12

获取可能的路径

340

关于提升的Javascript问答

17

简单的问答游戏JavaScript

36

JSON Javascript问答游戏

07
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档