简体   繁体   English

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

[英]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:在此循环内,执行以下操作:

  1. Detemine if the currentPage is an ending Create a function to do this.确定 currentPage 是否为结尾创建 function 来执行此操作。 That will keep your code nice and clean.这将使您的代码保持整洁。

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 来确定当前页面是否为结尾。

  1. If the page is an ending, then print that page and exit the game Since your while loop ends if currentPage is null, assign null to the current page and then your loop will end on the next iteration.如果页面是结尾,则打印该页面并退出游戏由于如果 currentPage 为 null 则循环结束,请将 null 分配给当前页面,然后您的循环将在下一次迭代中结束。

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 个明显的缺陷:

  1. The while loop inside the function is redundant, currentPage should never be null function 内的while循环是多余的,currentPage 永远不应该是 null
  2. You can use the first answer here to make a consecutive range of numbers much more easily您可以在这里使用第一个答案来更轻松地制作连续的数字范围

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

相关问题 等待异步函数返回true或false - 如何检查返回值? - Waiting for async function to return true or false - how do I check return value? 如何为 Javascript 中的三元运算符返回真或假? - How do I return true or false for ternary operator in Javascript? 如何实现return false; 进入JavaScript if / else - How do I implement return false; into JavaScript if/else 如何将“条纹”按钮与正确/错误检查一起使用? - How do I use my Stripe button with a true/false check? 我如何不断检查我的 if 语句是否返回 true 或 false - How do I constantly check if my if statement returns true or false 如何编写在AngularJS中返回true / false的服务? - How do I write a service that return true/false in AngularJS? 如何使用jQuery noty插件返回true或false? - How do I return true or false using jQuery noty plugin? 如何将 Boolean 作为表达式返回以替换 if、else true 和 false - How to return a Boolean as an expressiont to replace if, else true and false 我如何检查一个数组是否有 2 个或更多相同的值,如果有则返回 false? - How do i check if an array have 2 or more same value and return false if it does? 如果数组的所有值都为真,如何返回真,否则返回假? - How to return true if all values of array are true otherwise return false?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM