Skip to content

Workshop: Conditionals and Functions

Practical.

  1. Create a file named condiFunct.js and save it in a folder where you can run it with the node command, like node condiFunct.js.

  2. Create two global variables representing numbers. Also create a global boolean variable and set it to true.

  3. Define a new function. Create a local variable which is a number. Return the sum of the local variable and global number variables.

  4. Define another new function. Randomly change the global boolean variable to a different value. Using this global boolean as a condition, create some code that prints one thing to the console if it is true and another thing to the console if it is false.

  5. Call your first function, using it to declare and give a value to a new global number variable. Print a new line to the console which contains this new global number variable.

  6. Call your second function.

Creative.

  1. In a new JavaScript file (saved to the same folder as last time) add some code that uses at least one conditional and one function to create a story that is printed out to the console. The story must include variables such as numbers, strings, and or booleans.

  2. Using Math.random() with Math.floor() to produce a random integer, add a random aspect to your story. Use if ... else statements or switch to create lines printed to the console which form a story. Printing the story to console should produce a slightly different story each time the programme is run.

Content CC BY 4.0 | Code AGPL 3.0