[英]how do i check array number form "pages" to "endingPages" if they match return true else is false
when prompted i enter the number then it dont go thru the function it just makes current page = whatever i put when i get promted当提示我输入数字时,它不会 go 通过 function 它只是使当前页面=我收到提示时输入的任何内容
INSTRUCTIONS: Create a while loop, while(currentPage !== null)说明:创建一个while循环,while(currentPage !== null)
Inside of this loop, do the following:在此循环内,执行以下操作:
Have a single parameter for your function, currentPage.为您的 function,currentPage 提供一个参数。
Use a loop to check if the current page matches any of the page numbers in endingPages使用循环检查当前页面是否与endingPages中的任何页码匹配
If you find the page within endingPages, return true.如果您在endingPages 中找到该页面,则返回true。 If you do not, return false.
如果不这样做,则返回 false。
Use this function in your while loop to determine if the current page is an ending.在您的 while 循环中使用此 function 来确定当前页面是否为结尾。
Hint: It would probably look nice to print out something like "GAME OVER" or "The End" here... CODE提示:在这里打印出“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
}
}
}
}
I think there is a lot of misinterpretation here.我认为这里有很多误解。 Ultimately currentPage never changes because function parameters in Javasript are pass-by-value
最终 currentPage 永远不会改变,因为 Javasript 中的 function 参数是按值传递的
Either way in your code I do not see an attempt to print out currentPage after the user has been prompted and the function called无论哪种方式,在您的代码中,我都没有看到在提示用户并调用 function 后尝试打印出 currentPage
Ultimately your function should still return what you want, and you only need to test whether it returns true or false, by if (ending(currentPage)) { console.log("Game Over;"); }
最终你的 function 应该仍然返回你想要的,你只需要通过
if (ending(currentPage)) { console.log("Game Over;"); }
if (ending(currentPage)) { console.log("Game Over;"); }
2 additional obvious flaws in the provided code:提供的代码中还有 2 个明显的缺陷:
while
loop inside the function is redundant, currentPage should never be null while
循环是多余的,currentPage 永远不应该是 null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.