Assign the result of an addition to variable 'a' using an assignment block as follows:
"2 + 1 = 3; assigning the value
"Calling the 'print' function, Printing the value of variable 'a' "
Now try performing other additions and other operations.
Subtraction: " 3 - 1 = 2; assigning the value '2' to variable 'a'
Multiplication: "3 x 2 = 6; assigning the value '6' to variable 'a' "
Use the symbol '*' to multiply two numbers.
Division: "6 / 2 = 3; assigning the value '3' to variable 'a' "
Use the symbol '/' to divide two numbers.
Remainder of a division: "5 / 3 = 1 with a remainder of 2; therefore 5 % 3 = 2; assigning the value '2' to variable 'a' "
Use the symbol '%' (modulo operator) to calculate the remainder of a division between two numbers.
Adding the return of the 'random' function to a number: "Calling 'random' function; the result of the call is '3'; 3 + 2 = 5; assigning the value '5' to variable 'a' "
To modify the value of a variable, assign a value to variable 'a' using an assignment block, afterwards assign another value to variable 'a' using another assignment block.
Example:
Place the blocks as follows
"Assigning value 2 to variable 'a' "
Continue reading the program in the direction of the arrows
"Assigning value 6 to variable 'a' ". You've just modified the value of variable 'a' !
Yes, you can:
- " 3 x 2 = 6; assigning the value '6' to variable 'a' "
- "Calling the 'print' function; printing the value of 'a' on the board."
- "Given a = 6; then: a - 1 = 5; assigning the value '5' to variable 'a' "
- "Calling the 'print' function; printing the value of 'a' on the whiteboard."
Write the programs in the editor and press RUN to execute them.
Note: We have chosen JavaScript as the programming language so that you don't have to install anything on your computer.
Use curly braces { } to delimit code blocks. These blocks can contain multiple instructions.
Code comments: Use ' // ' and ' /* */ ' to comment the code.
Assign value:Declare a variable 'a' and assign the value 3. Then print the value of 'a' using 'console.log'.
Addition: Print the result of an addition:
Other operations: Print the result of other operations: Modify the value of 'a' in each operation.
Assign a random value to a variable: Declare a variable 'a' and assign a random value between 1 and 6 (by calling the 'random' function).
Then print the value of 'a' using 'console.log'.
Next, do the same but subtract a number from the result of the 'random' function call.
Using variables in operations: Assign a value to variable 'a' and increment it by 1, then multiply it by 3:
Using variables in operations (2): Assign a value to a variable 'a'. Then assign a value to a variable 'b'. Finally sum the two values and print the result:
Read the following code and answer the question: What will be the value of variable 'a' once the two assignments have been executed?
Answer:
4Using the following blocks, write a code where the variable 'a' starts at 3 and ends at 12.
Possible solution:
What is the value of variable 'a' once all assignments have been executed?
Answer:
2Complete the ' // ... ' in the code with assignments so that variable 'a' ends up being equal to 7.
Possible solution:
"Thank goodness you're here, I have a problem! Can you fix the code so that the program prints the number '10' instead of '5' after going through the three assignments? "
Possible solution:
"Something went wrong! I expected that after executing the following code, the value of 'a' would be 1, but it's not. Can you fix the code by changing only one arithmetic operator, so that 'a' equals 1 after going through the five assignments?"
Possible solution: