Lab JS1 JavaScript Basics (Expressions and Assignment Statements)




      Part 1: Evaluate each of the following expressions.


      1. 3 * (-5)
      2. 4 + 2 * 5 - 10
      3. 5 / 2
      4. Math.floor(5/2)
      5. 9 % 3
      6. -21 % -2
      7. 21 % -2
      8. 4 + 5 * 2 + 3
      9. (4 + 5) * 2 + 3
      10. 4 * 5 / 2 + 5 % 2
      11. -3 * (( 4 + 2 ) * 2.0 ) - 1
      12. 5 / (20 - (100 / 5))
      13. 1 % -2
      14. -3 % 8
      15. 4 * 3 / 4 + 12
      16. 1 + 25 % 5
      17. 1 % (6 % 3)
      18. 12 % 8 % 20 % 15





JavaScript online sandbox: https://codehs.com/explore/sandbox/javascript

Part 2: Strings



Show the final variable values and the output for the following code segments.

  1. let str1 = "Bob";
    let str2 = "Cat";
    let str3 = str2 + "\n" + str1 + str2;
    console.log( str1 + str2 + str3);


  2. let str1 = "Bob";
    let str2 = "Cat";
    let str3 = str1 + " three " + str2;
    let count = str3.length;
    console.log( str1 + str2 + str3 + count);


  3. let str1 = "HotFrenchFries"
    let str2 = str1.charAt(0)
    let c2 = str1.charAt(4)
    console.log( str1 + str2 + c2);


  4. let str1 = "Hot FrenchFries"
    let str2 = str1.charAt(0)
    let c2 = str1.charAt(4)
    console.log( str1 + str2 + c2);




Part 3: Assignment Statements





      // CONSTANTS
      // CONSTANTS
      const MIN = 2;
      const MAX = 5;


      // VARIABLES
      let y = 7
      let z = 2
      let x = 20
      let fNum = 100
      let t = 4
    Show the assignment values, or errors, that occur in the following sequence of instructions.

  1. x = Math.floor(y/z) + 4;
  2. x = x + y * 3;
  3. x = 2 / (t - 4);
  4. x = y % 3 + MIN / t + MAX;
  5. x = 3 - 4 * x;
  6. fNum = MAX / MIN;
  7. fNum = y % 2;
  8. 4 / 2 + fNum = fNum;
  9. y += z;
  10. MIN = MAX + 21/3;
  11. y = -10 / 2 % 4;
  12. x = Math.sqrt(98 + MIN) / MAX;
  13. x++;
  14. --y;