In some languages it’s easy to do if you are testing it as you go. In other languages it’s much more difficult.
For example, in Python you could do:
userInput = input(“Enter a number: “)
lenCheck = True
numCheck = True
if len(userInput) != 11:
lenCheck = False
for letter in userInput:
if not letter.isdigit():
numCheck = False
if not numCheck:
print(“There were non-numerical digits entered”)
elif not lenCheck:
print(“The length of a phonenumber must be 11 digits”)
else:
print(“Thank you”)
(I’m guessing the indents won’t print properly, but you can get the idea).
I think it’s far too difficult a question for pupils to write out the answer. If they were given a computer and Internet access, that would be fair.
Even marking this question would be an utter nightmare. Your sample JavaScript answer may well be perfect, but I can’t tell just by looking at it (I’m not a JS expert). Will all markers be versed in all languages and be able to tell if someone has sufficiently validated the input?
Like you, I can’t wait to see the solution. I can’t envisage there being a ‘one size fits all’ answer that pupils could genuinely be expected to provide. Then again, I’m often wrong 🙂