There are 10,000 possible combinations for 4-digit pin number using digits 0-9. In this program, we'll see how we can use loops to guess that a 4-digit pin in only a few seconds.Create a global variable called pinCode and set it to a 4-digit code using 0-9. Since we want the pin code to support combinations starting with 0, we'll want to use a string instead of a number:var pinCode = "0049";Create a function called check that takes a possible combination in as a parameter and returns either true or false.Write a loop (for or while) that iterates through every number from 0 - 9999, checking each one for the combination. The loop should terminate if it guesses correctly or if it reaches 9999.Use the provided leftPad function to turn your numbers in to 4-digit pins with leading 00's. For example: var a = 10; var as = leftPad(10, 4); // as is now "0010" Additional instructions:The leftPad function is designed to turn a number into a 4-digit string that we can attempt to match against the pinCode variable. It is fully implemented at the beginning of the assignment and does not need to be modified. It takes 2 parameters: a number and the desired length (which will be 4 in our case).The check function is supposed to take a number, get the 4-digit string combination using the leftPad function and compare it to the pinCode variable. It will return true if it is equal and false otherwise.Your while loop has 2 conditions: found is false and the count is less than 10,000. The basic strategy here is to increment count by 1 every time this loop runs, put it in the check method and set the result = to found. Either found will become true (if the pinCode is valid) or count will reach 10,000 (pinCode is not valid).If found is true after you exit the while loop, display an alert with the discovered pin code.
You'll get 1 file (1.6KB)