[英]Waiting for async function to return true or false - how do I check return value?
[英]how do i check array number form "pages" to "endingPages" if they match return true else is false
当提示我输入数字时,它不会 go 通过 function 它只是使当前页面=我收到提示时输入的任何内容
说明:创建一个while循环,while(currentPage !== null)
在此循环内,执行以下操作:
为您的 function,currentPage 提供一个参数。
使用循环检查当前页面是否与endingPages中的任何页码匹配
如果您在endingPages 中找到该页面,则返回true。 如果不这样做,则返回 false。
在您的 while 循环中使用此 function 来确定当前页面是否为结尾。
提示:在这里打印出“GAME OVER”或“The End”之类的内容可能看起来不错……代码
console.log(pages[0]);
let endingPages = [4, 9, 13, 17, 19, 20];
let currentPage = 0;
let pages = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
// Your Code Here.
currentPage = prompt(" enter page :")
ending(currentPage);
function ending (currentPage){
while(currentPage !== null) {
for(i = 0 ; i <= endingPages.length ; i ++ ){
currentPage = endingPages[i]
if (endingPages[i] === pages.length){
return true
}else{
return false
}
}
}
}
我认为这里有很多误解。 最终 currentPage 永远不会改变,因为 Javasript 中的 function 参数是按值传递的
无论哪种方式,在您的代码中,我都没有看到在提示用户并调用 function 后尝试打印出 currentPage
最终你的 function 应该仍然返回你想要的,你只需要通过if (ending(currentPage)) { console.log("Game Over;"); }
if (ending(currentPage)) { console.log("Game Over;"); }
提供的代码中还有 2 个明显的缺陷:
while
循环是多余的,currentPage 永远不应该是 null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.