繁体   English   中英

我如何检查数组编号从“pages”到“endingPages”,如果它们匹配返回true,否则为false

[英]how do i check array number form "pages" to "endingPages" if they match return true else is false

当提示我输入数字时,它不会 go 通过 function 它只是使当前页面=我收到提示时输入的任何内容

说明:创建一个while循环,while(currentPage !== null)

在此循环内,执行以下操作:

  1. 确定 currentPage 是否为结尾创建 function 来执行此操作。 这将使您的代码保持整洁。

为您的 function,currentPage 提供一个参数。

使用循环检查当前页面是否与endingPages中的任何页码匹配

如果您在endingPages 中找到该页面,则返回true。 如果不这样做,则返回 false。

在您的 while 循环中使用此 function 来确定当前页面是否为结尾。

  1. 如果页面是结尾,则打印该页面并退出游戏由于如果 currentPage 为 null 则循环结束,请将 null 分配给当前页面,然后您的循环将在下一次迭代中结束。

提示:在这里打印出“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 个明显的缺陷:

  1. function 内的while循环是多余的,currentPage 永远不应该是 null
  2. 您可以在这里使用第一个答案来更轻松地制作连续的数字范围

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM