Solution code for Express drills
-
Create a route handler function on the path
/sum
that accepts two query parameters nameda
andb
and find the sum of the two values. Return a string in the format "The sum of a and b is c". Note that query parameters are always strings so some thought should be given to converting them to numbers. -
Create an endpoint
/cipher
. The handler function should accept a query parameter namedtext
and a one namedshift
. Encrypt the text using a simple shift cipher also known as a Caesar Cipher. It is a simple substitution cipher where each letter is shifted a certain number of places down the alphabet. So if the shift was 1 then A would be replaced by B, and B would be replaced by C and C would be replaced by D and so on until finally Z would be replaced by A. using this scheme encrypt the text with the given shift and return the result to the client. -
To send an array of values to the server via a query string simply repeat the key with different values. For instance, the query string
?arr=1&arr=2&arr=3
results in the query object{ arr: [ '1', '2', '3' ] }
. Create a new endpoint/lotto
that accepts an array of 6 numbers between 1 and 20. The function then randomly generates 6 numbers between 1 and 20. Compare the numbers sent in the query with the randomly generated numbers to determine how many match. If fewer than 4 numbers match respond with the string "Sorry you lose". If 4 numbers match respond with the string "Congratulations, you win a free ticket", if 5 numbers match respond with "Congratulations! You win $100!". If all 6 numbers match respond with "Wow! Unbelievable! You could have won the mega millions!".