Tue Oct. 17

JavaScript Control Flows & Arrays

9 – 12h Teaching

Teacher:

  • Thomas Brüderli

Goals

    • I know how to use the if/else statement and its operators
    • I know the difference between "true", "truthy" and "falsey"
    • I know how to use the for-loop
    • I know how to declare arrays as well as assigning, changing, removing and reading values in arrays

RECAP – 16.10.

  1. JavaScript functions & variables
  2. Data Types: primitives, strings, loose typing
  3. Q&A

12 – 13.30h Lunch (Kantine im 5i)

13.30 – 17h Coaching

Coaches:

  • Jörg Wasmeier

Learning by Doing

Exercises

The Word Guesser

You'll create a simple word guessing game where the user gets infinite tries to guess the word (like Hangman without the hangman, or like Wheel of Fortune without the wheel and fortune).

  • Create two global arrays: one to hold the letters of the word (e.g. 'F', 'O', 'X'), and one to hold the current guessed letters (e.g. it would start with '_', '_', '_' and end with 'F', 'O', 'X').
  • Write a function called guessLetter that will:
    • Take one argument, the guessed letter.
    • Iterate through the word letters and see if the guessed letter is in there.
    • If the guessed letter matches a word letter, changed the guessed letters array to reflect that.
    • When it's done iterating, it should log the current guessed letters ('F__')
    • and congratulate the user if they found a new letter.
    • It should also figure out if there are any more letters that need to be guessed,
    • and if not, it should congratulate the user for winning the game.
  • Pretend you don't know the word, and call guessLetter multiple times with various letters to check that your program works.
  • Bonus: Make it more like Wheel of Fortune:
    • Start with a reward amount of $0
    • Every time a letter is guessed, generate a random amount and reward the user if they found a letter (multiplying the reward if multiple letters found), otherwise subtract from their reward.
    • When they guess the word, log their final reward amount.
  • Bonus: Make it like Hangman:
    • Keep track of all the guessed letters (right and wrong) and only let the user guess a letter once. If they guess a letter twice, do nothing.
    • Keep track of the state of the hangman as a number (starting at 0), and subtract or add to that number every time they make a wrong guess.
    • Once the number reaches 6 (a reasonable number of body parts for a hangman), inform the user that they lost and show a hangman on the log.

[ Source | Solution on repl.it ]

The Converter

  • Write a function that does a one way imperial to metric conversion with the value to be converted passed in as an argument.
  • Update your conversion function so that values can be converted both ways, with the type of conversion also passed in as an argument. Your function call should look like this:
convert(15, "inches to centimeters");
  • Hint - use an 'if' statement to check the type of conversion.
  • Update your conversion function so that an array of values can be passed in as the first argument with each value converted.

[ Solution on repl.it ]