简体   繁体   中英

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

INSTRUCTIONS: Create a while loop, while(currentPage !== null)

Inside of this loop, do the following:

  1. Detemine if the currentPage is an ending Create a function to do this. That will keep your code nice and clean.

Have a single parameter for your function, currentPage.

Use a loop to check if the current page matches any of the page numbers in endingPages

If you find the page within endingPages, return true. If you do not, return false.

Use this function in your while loop to determine if the current page is an ending.

  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.

Hint: It would probably look nice to print out something like "GAME OVER" or "The End" here... CODE

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

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

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;"); } if (ending(currentPage)) { console.log("Game Over;"); }

2 additional obvious flaws in the provided code:

  1. The while loop inside the function is redundant, currentPage should never be null
  2. You can use the first answer here to make a consecutive range of numbers much more easily

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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