Quantcast
menu-button

Characters

Tasks

As per the slides above, there are two character-related JavaScript functions that you need to know about for these tasks.

  • "A".charCodeAt(0) converts the character 'A' into its corresponding integer value, in this case 65. The 0 in the brackets is the character position (starting from 0) of the character in the string "A" that you want the integer value for. For example, "HORSE".charCodeAt(4) would get the integer value for the character 'E'. If you just want the integer value a single character, just use 0 in the brackets.
  • String.fromCharCode(65) would give the character corresponding to the integer 65 (which we know from above is capital 'A').

Task 1a: Converting from characters to integers.

Write a function that takes a character and returns the integer representation of it.

Reset to Original Code     Fullscreen

Task 1b: Converting from integers to characters.

Write a function that takes an integer and returns the character .

Reset to Original Code     Fullscreen

Task 2a: Letter After.

Write a function that takes a letter and gives you the letter after it. We recommend you follow the following algorithm:

  1. Convert the letter to an integer.
  2. Add one to the integer.
  3. Convert the number back into a character. This works because consecutive letters are represented by consecutive numbers.
Reset to Original Code     Fullscreen

Task 2b: Letter Between.

Write a function that takes two letters and gives the letter halfway between them. For example, the letter halfway between 'a' and 'e' is 'c':

Reset to Original Code     Fullscreen

Task 3a: Output Letters in a Range

Write code that outputs the characters corresponding to integers in some range of your choice. For example, if your two numbers were 65 and 68, your code should output "65 = A, 66 = B, 67 = C, 68 = D".

You should use a for loop.

Reset to Original Code     Fullscreen

Task 3b: Output Letters in a Range where Characters are Entered

Do the same as above, but your code should first ask the user for each of two characters, i.e. "Enter the character to start from." and "Enter the character to go up to".

Reset to Original Code     Fullscreen

Task 4: Secret Code!

You want to 'encrypt' words by replacing each letter with the one above it. So encrypt("Hello") would output "Ifmmp". Write a function encrypt which does this. Again, you should consider using a for loop that builds up your encrypted word character by character.

Reset to Original Code     Fullscreen