Exercise 1B: JavaScript Problem Solving

Write JavaScript code to solve the following problems (you may recognise these …). Where a code asks for numerical input from a user you can just ‘hard-code’ this input into your answer:

  1. Print the result and the type of the following:

    • 5
    • 5 * 2
    • 5 / 2
    • 5 % 2
    • 5**2
    • 5.0
    • 5.0 * 2
    • 5.0 / 2
    • 5.0 % 2
    • 5.0**2
    • ‘good’
    • ‘good’ + ‘morning’
    • ‘good’*3
    • ‘3’ + ‘2’
    • ‘3’ * 2
    • ‘3’ + 2
    • ‘3’ * ‘2’
  2. Follow these instructions:

    • Store the values 3 and 5 into variables a and b, respectively.
    • Multiply a and b and store the result into c.
    • Divide b by a and store the results into d
    • Print a, b, c, and d.
    • Update the values of a, b, c, and d as follows:
      • Divide c by b.
      • Multiply d by a.
      • Increment a by 1.
      • Subtract 1 from b.
    • Store the sum of a, b, c, and d into res.
    • Print res
  3. Define three integer values, a, b, and c. Then, verify the following logical conditions and print their result:

    • a is greater than b.
    • b is greater than or equal to a and lower than or equal to c.
    • c is lower than a or greater than b.
    • a is not equal to b or equal to c.
    • The sum of a, b, and c is not greater than 5.
  4. Definte three integer values, a, b, and c. Then, translate the following statements into code and test them on the user’s inputs:

    • If the sum of a, b, and c is less then 10, then add 5 to a and print “The value of A has been updated to [value]” (example: “The value of A has been updated to 6”); otherwise, print “Test 1 passed”.
    • If a is greater than c, then check if a is greater than b. If that’s the case, print “A is the greatest”; otherwise print “A is not the greatest”. If a is not greater than c, then print “C might be greater than A and B”.
    • Define a fourth number, d. If d is equal to a or b or c, print “You already introduced this number”. Otherwise, print “Thanks for your contribution”.
    • Define a word, w. If w is “sum”, print “The sum of A, B, and C is [sum]”. If w is “hello”, then print “Hi! Nice to meet you!”. If w is greater than or equal to “a” and lower than or equal to “z”, print “This is just a letter…”. Finally, if the user writes something else, print “Sorry, I don’t understand.”.
  5. Using for loops:

    • Print the numbers from 0 to 9.
    • Print the numbers from 1 to 9.
    • Print the numbers from 1 to 10.
    • Print the even numbers from 2 to 20 (inclusive).
    • Print the sum of all the even numbers from 1 to 21.
    • Given numbers = [1, 2, 3, 4, 5], print the squared value of each item in numbers.
    • Given numbers = [1, 4, 7, 8, 15, 20, 35, 45, 55], sequentially print the items in numbers. Stop when you reach 15. Note: you need to use if and break.
  6. Write some JavaScript to print the following string in a specific format.

Sample String : “Twinkle, twinkle, little star, How I wonder what you are! Up above the world so high, Like a diamond in the sky. Twinkle, twinkle, little star, How I wonder what you are”

Output :

Twinkle, twinkle, little star,
    How I wonder what you are! 
        Up above the world so high,           
        Like a diamond in the sky. 
Twinkle, twinkle, little star, 
    How I wonder what you are
  1. Write some JavaScript to get the the volume of a sphere, given its radius r.

  2. Write some JavaScript to convert any number of days, hours, minutes and seconds into seconds.