February 28, 2025
JavaScript Password exercises
This question tests a range of programming concepts including, boolean conditions, branching and looping.
Q6.
A JavaScript program is required to request, via a dialogue box, a password
from a user. The input, which can be of any length, must be validated such
that it is only valid if it has no spaces. Any character, other than a space,
is permissible. As soon as a space is detected, the program should terminate
with a message provided in an alert box stating
Invalid, contains a space!
If the password is deemed correct the program should output, via an alert
box, the message
Valid password.
The next two questions test your knowledge of functions; namely how to write and call them.
Q7.
The code for the above Q6 is to be modified, although it will still implement
the same behaviour. When modifying your code you are asked to write and use
a function isSpace(). This function will accept a string as its
argument. If the string is a single space character, the function will return
true, else it returns false.
Q8.
The code for Q6 is to be further modified. The user is to be initally presented
with a dialogue box displaying the message
Do you wish to enter a password?
If the user confirms by clicking OK, the dialogue box requesting a password
is to be provided. However, if the user declines, your program should immediately
exit with a the message
Access denied.
Your code should implement and make use of a function checkEnter(). This function returns, depending on the user's response to the initial dialogue confirm, an appropriate Boolean value.
Next page » Arrays
Previous page « Booleans
⇑ Up to top of page