JavaScript Practice Exercises for Midterm - Solutions, Study notes of Algorithms and Programming

Solutions to the JavaScript practice exercises for a midterm exam. It includes evaluating expressions, converting binary and decimal numbers, writing JavaScript code, and tracing code execution. The exercises cover topics such as arithmetic operations, loops, functions, and user input.

Typology: Study notes

2021/2022

Uploaded on 08/05/2022

nguyen_99
nguyen_99 ๐Ÿ‡ป๐Ÿ‡ณ

4.2

(80)

1K documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1๎˜ƒ
COMP๎˜ƒ110๎˜ƒPractice๎˜ƒExercises๎˜ƒfor๎˜ƒMidterm๎˜ƒโ€“๎˜ƒSolutions๎˜ƒ
Answers๎˜ƒare๎˜ƒhighlighted๎˜ƒin๎˜ƒblue๎˜ƒcolor.๎˜ƒ
Evaluate๎˜ƒeach๎˜ƒof๎˜ƒthe๎˜ƒfollowing๎˜ƒJavascript๎˜ƒexpressions๎˜ƒand๎˜ƒshow๎˜ƒthe๎˜ƒvalue.๎˜ƒ
1. โ€9*3๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒโ€27๎˜ƒ
2. โ€œvalue๎˜ƒis๎˜ƒโ€œ๎˜ƒ+๎˜ƒ50๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒโ€œvalue๎˜ƒis๎˜ƒ50โ€๎˜ƒ
3. 17๎˜ƒ%๎˜ƒ5๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ2๎˜ƒ
4. 5๎˜ƒ%๎˜ƒ17๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ5๎˜ƒ
5. 5/10๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ0.5๎˜ƒ
6. (4๎˜ƒ==๎˜ƒ4)๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒtrue๎˜ƒ
7. (4๎˜ƒ!=๎˜ƒ5)๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒtrue๎˜ƒ
8. (7๎˜ƒ<=๎˜ƒ8)๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒtrue๎˜ƒ
9. Math.ceil(x)๎˜ƒโ€๎˜ƒMath.floor(x)๎˜ƒ๎˜ƒ 1๎˜ƒ
Convert๎˜ƒthese๎˜ƒbinary๎˜ƒnumbers๎˜ƒinto๎˜ƒdecimal๎˜ƒnumbers.๎˜ƒ
10. 110111๎˜ƒ๎˜ƒ 55๎˜ƒ
11. 101011๎˜ƒ๎˜ƒ 43๎˜ƒ
12. 10100๎˜ƒ๎˜ƒ 20๎˜ƒ
Convert๎˜ƒthese๎˜ƒdecimal๎˜ƒvalues๎˜ƒinto๎˜ƒbinary๎˜ƒnumbers.๎˜ƒ
13. 55๎˜ƒ๎˜ƒ 110111๎˜ƒ
14. 43๎˜ƒ๎˜ƒ 101011๎˜ƒ
15. 20๎˜ƒ๎˜ƒ 10100๎˜ƒ
Write๎˜ƒjavascript๎˜ƒcode๎˜ƒto๎˜ƒdo๎˜ƒthe๎˜ƒfollowing.๎˜ƒ
16. Alert๎˜ƒ"Hello๎˜ƒworld."๎˜ƒ
alert("Hello world");
17. Read๎˜ƒa๎˜ƒnumber๎˜ƒ(using๎˜ƒprompt)๎˜ƒand๎˜ƒdisplay๎˜ƒit๎˜ƒusing๎˜ƒalert.๎˜ƒ
var n = prompt("Please enter a number.");
alert("Your number is "+ n);
18. Read๎˜ƒtwo๎˜ƒnumbers๎˜ƒand๎˜ƒdisplay๎˜ƒtheir๎˜ƒproduct.๎˜ƒ
var n1 = prompt("Please enter a number.");
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download JavaScript Practice Exercises for Midterm - Solutions and more Study notes Algorithms and Programming in PDF only on Docsity!

COMP 110 Practice Exercises for Midterm โ€“ Solutions

Answers are highlighted in blue color.

Evaluate each of the following Javascript expressions and show the value.

  1. โ€9*3 โ€ 27
  2. โ€œvalue is โ€œ + 50 โ€œvalue is 50โ€
  3. 17 % 5 2
  4. 5 % 17 5
  5. 5/10 0.
  6. (4 == 4) true
  7. (4 != 5) true
  8. (7 <= 8) true
  9. Math.ceil(x) โ€ Math.floor(x) 1

Convert these binary numbers into decimal numbers.

  1. 110111 55
  2. 101011 43
  3. 10100 20

Convert these decimal values into binary numbers.

  1. 55 110111
  2. 43 101011
  3. 20 10100

Write javascript code to do the following.

  1. Alert "Hello world." alert("Hello world");
  2. Read a number (using prompt) and display it using alert. var n = prompt("Please enter a number."); alert("Your number is "+ n);
  3. Read two numbers and display their product. var n1 = prompt("Please enter a number.");

var n2 = prompt("Please enter another number."); alert("The product of "+n1+" and "+n2+" is "+ n1*n2);

  1. Read two numbers and display their sum. What problem did you encounter? var n1 = 1prompt("Please enter a number."); var n2 = 1prompt("Please enter another number."); alert("The sum of "+n1+" and "+n2+" is "+ n1+n2); If you leave out multiplying the numbers by 1, you will get concatenation instead of addition since prompt returns a string.
  2. Read in two numbers and display the larger. var n1 = 1prompt("Please enter a number."); var n2 = 1prompt("Please enter another number."); if (n1 > n2) alert(n1+" is the larger."); else alert(n2+" is the larger."); If you leave out the multiplication by 1, you will get strange answers since prompt returns a string. For example, while the number 100 is greater than the number 99, the string "99" is greater than the string "100".
  3. Read in two numbers and display them in ascending order. var n1 = 1prompt("Please enter a number."); var n2 = 1prompt("Please enter another number."); if (n1 > n2) alert(n2+" "+n1); else alert(n1+" "+n2);
  4. Use a loop to display the numbers 0 through 5, each in a separate alert window. for (var i=0; i<=5; i++) { alert(i); }
  5. Use a loop to display the numbers 0 through 5 in a single alert window.

// Solution 2: Put the test inside the loop. while (true) { var n = prompt("Please enter a number in the range 0โ€ฆ100"); if (n>=0 && n<=100) break; alert(n +" is out of range. Try again."); }

  1. Repeat previous exercise, but this time allow for the possibility that the user enters something that is not a number. Hint: the built in function isNaN(x) returns true if x is not a number. It returns false if x is a number. Sort of backwards, but that's what's available. // Solution 1: put the test in the while statement. var n = prompt("Please enter a number in the range 0โ€ฆ100"); while (isNaN(n) || n<0 || n>100) { alert(n +" is out of range or not a number. Try again."); n = prompt("Please enter a number in the range 0โ€ฆ100"); } // Solution 2: put the test inside the loop. while (true) { var n = prompt("Please enter a number in the range 0โ€ฆ100"); if (!isNaN(n) && n>=0 && n<=100) break; alert(n +" is out of range or not a number. Try again."); }
  2. Prompt for an integer, then display the sum of the integers from 0 through the number entered. For example, if you enter 10, then display 55 which is the sum of 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10. // Get the upper bound. var max = 1*prompt ("Please enter an integer."); var sum = 0; for (var i=0; i<=max; i++)

sum = sum + i; // Add i to the running sum. alert("The sum of 0 through "+max+" is "+sum);

  1. Prompt for an integer, then display the average of the integers from 0 through the number entered. For example, if you enter 10, then display 5 which is the average of (0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10)/11. // Get the upper bound. var max = 1*prompt ("Please enter an integer."); var sum = 0; for (var i=0; i<=max; i++) sum = sum + i; // Add i to the running sum. alert("The average of 0 through "+max+" is "+(sum/(max+1))); Write a function to do each of the following.
  2. greet(); displays "Hello world" function greet() { alert("Hello world"); }
  3. sum(n); displays the sum of 0+1+2+โ€ฆ+n. // Sum the integers 0โ€ฆn. function sum(n) { var tot=0; for (var i=0;i<=n;i++) tot=tot+i; alert("The sum of 0 through "+n+" is "+tot); }
  4. isValid(n) returns true if n is a number 0โ€ฆ100. // Is the parameter between 0 and 100? function isValid(n) { // Restriction: none // Errors checked for: n not a number;

var x =5; var y =1;

while (x > 0){ x = x-1; y = y*x; alert(x + " " + y); }

var i;

var count =0; for (i =0; i < 11; i++){ if (i < 3 || 7 <i){ count++; alert(count + " i " + i + " range 1"); }else if (i == 5){ count++; alert(count + " i " + i + " range 2"); } if ( 2<= i && i < 7){ count++; alert(count + " i " + i + " range 3"); } }

1 i 0 range 1

2 i 1 range 1

3 i 2 range 1

4 i 2 range 3

5 i 3 range 3

6 i 4 range 3

7 i 5 range 2

8 i 5 range 3

9 i 6 range 3

10 i 8 range 1

11 i 9 range 1

12 i 10 range 1

  1. What is the output displayed by each of the following code fragments? for (var i=0; i<10; i++) { alert(i); } 10 alerts with the values 0โ€ฆ9. for (var i=10; i<10; i++) { alert(i); } Nothing. The loop condition fails immediately. for (var i=10; i>=0; i--) { alert(i) } 11 alerts with the values 10 down to 0. for (var i=0; i<5; i++) { for (var j=0; j<3; j++) { alert(i +" "+ j); } } 15 alerts with the following values.

1 1 1 2 1 3 1 4 2 2 2 3 2 4 3 3 3 4 4 4

  1. What does this code do?

var count=0; for (var half=0; half<=2; half++) { for (var qtr=0; qtr<=4; qtr++) { for (var dime=0; dime<=10; dime++) { for (var nick=0; nick<=20; nick++) { for (penny=0; penny<=100; penny++) { if (50half + 25qtr + 10dime + 5nick + penny == 100) { count++; } } } } } } alert(count); It counts and displays the number of different way to make $1.00 using half dollars, quarters, dimes, nickels, and pennies. It tests 349,965 different combinations to find the 292 combinations that add up to exactly $1.00.