Top Banner
Q1: which of the following identifiers represents an invalid variable name, and why? myNumber my.Number my-Number my_Number my Number $myNumber myNumber2 8myNumber While Q2: Write a JavaScript code to declare a variable called myNumber; initialize it with the number 7; add 3 to its value; and finally display the new value. (M150A final exam, Fall 2008) var myNumber; myNumber = 7; myNumber = myNumber + 3; document.write('The new value of myNumber is ' + myNumber); Q3: Write a JavaScript code to do the following: Ask the user to enter his / her name Display on the screen: Welcome followed by the user name var userName; userName = window.prompt('Please enter your name',''); document.write('Welcome ' + userName); Q4: Write a JavaScript code to compute and print the perimeter of a rectangle as follows: Ask the user to enter the length and width of the rectangle Compute the perimeter of the rectangle based on the formula: Perimeter = 2 (length + width) Print the computed perimeter in the output window
30

1 Answer Unit 7,81 Answer Unit 7,81 Answer Unit 7,81 Answer Unit 7,81 Answer Unit 7,8

Oct 19, 2015

Download

Documents

R4nd3l

1 Answer Unit 7,81 Answer Unit 7,81 Answer Unit 7,81 Answer Unit 7,81 Answer Unit 7,81 Answer Unit 7,81 Answer Unit 7,81 Answer Unit 7,81 Answer Unit 7,8
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

Q1: which of the following identifiers represents an invalid variable name, and why?myNumber

my.Number

my-Number

my_Number

my Number

$myNumber

myNumber2

8myNumber

While

Q2: Write a JavaScript code to declare a variable called myNumber; initialize it with the number 7; add 3 to its value; and finally display the new value. (M150A final exam, Fall 2008)var myNumber;myNumber = 7;myNumber = myNumber + 3;document.write('The new value of myNumber is ' + myNumber);

Q3: Write a JavaScript code to do the following: Ask the user to enter his / her name Display on the screen: Welcome followed by the user namevar userName;

userName = window.prompt('Please enter your name','');

document.write('Welcome ' + userName);

Q4: Write a JavaScript code to compute and print the perimeter of a rectangle as follows: Ask the user to enter the length and width of the rectangle Compute the perimeter of the rectangle based on the formula:Perimeter = 2 (length + width) Print the computed perimeter in the output windowvar length, width, perimeter;length = window.prompt('Please enter the rectangle length','');length = parseFloat(length);width = window.prompt('Please enter the rectangle width','');width = parseFloat(width);perimeter = 2 * (length + width);document.write('The rectangle perimeter is ' + perimeter)

Q5: Write an equivalent JavaScript code to the following flow chart:

var grade;

grade = window.prompt('Please enter the grade','');grade = parseFloat(grade);

if (grade >= 90)

{document.write("Excellent");}else{document.write("Done");}

Q6: Write a JavaScript code to identify if a student passes specific course or failedBased on his final score according to the following: Read the final score from the user. If the final score is greater than or equal to 50, print on the screen Passed, otherwise print Failed.var score;

score = window.prompt('Please enter your score','');score = parseFloat(score);

if (score >= 50)

{document.write("Passed");}else{document.write("Failed");}

Q7: Write an equivalent JavaScript code to the following flow chart:

var grade;

grade = window.prompt('Please enter the grade','');grade = parseFloat(grade);

if (grade < 50)

{document.write('Failed' + '
' + 'Finished');}else{document.write('Passed' + '
' + 'Finished');}

Q8: Write a JavaScript code to identify if a student passes a specific course or failed based on his final score according to the following: Read the final score from the user Check the score, if it is less than 0 or greater than 100, print Invalid Score on the output screen, otherwise complete as follows: If the final score is greater than or equal to 50, print on the screen Passed, otherwise print Failedvar score;

score = window.prompt('Please enter your score','');score = parseFloat(score);

if (score < 0 || score > 100)

{document.write("Invalid Score");}else{if (score >= 50){document.write("Passed");}else{document.write("Failed");}}

Q9: Write a JavaScript code that will assign a grade to a student, based on his final score according to the following table:Score rangeGrade

90 - 100A

80 89B

70 79C

50 69D

0 49F

The program works as follows: Read the students final score. (Assume that the user will insert a score between 0 and 100) Assign the grade to the student according to the above table. Print the grade in the output window.var score, grade;score = window.prompt('Enter your score','');score = parseFloat(score);if (score >= 90){grade = 'A'}else{if (score >= 80){grade = 'B'}else{if (score >= 70){grade = 'C'}else{if (score >= 50){grade = 'D'}else{grade = 'F'}}}}document.write('The grade of ' + score + ' is ' + grade)

Q10: Write an equivalent JavaScript code to the following flow chart:

document.write('Hello' + '
');

var grade;

grade = window.prompt('Please enter the grade','');grade = parseFloat(grade);

if (grade < 0 || grade > 100)

{document.write('The grade is not correct');}else{if (grade >= 50){document.write('Passed');}else{document.write('Sorry ' + 'Failed');}}document.write('
' + 'Thank you')

Q11: Trace the following JavaScript code giving the exact output:var x, y;y = 7;x = 1 + 4 + 8;document.write('First, x = ' + x + ' and y = ' + y);document.write('
');

x = y + 2;x = x + 6;x = 3 + x / 3;document.write("Now, x = " + x + 'and y = ' + y + "
");

x = y;y = 9;// y = y + 10;document.write("Finally, x = " + x + "and y = " + y);

Answer: First, x = 13 and y = 7Now, x = 8and y = 7Finally, x = 7and y = 9

Q12: Trace the following JavaScript code giving the exact output:var w, y, z, text1, text2, text3, text4;w = '12'; x = '5';y = 12; z = 5;text1 = "CAR"; text2 = "car";text3 = "cat"; text4 = "cairo";

if ((w < x) && (y > z))document.write("First" + "
");elsedocument.write("Second" + "
");document.write("Guess" + "
");if ((text1 == text2) || (text3 < text4))document.write("Third" + "
");elsedocument.write("Fourth" + "
");if ((z != 10) && (text2 < text3)){z = z * 10;document.write("Finally, it is a " + text2);document.write(" for " + y + z);}else{z = z * 10;document.write("See you");}document.write("
" + "The End - " + "text4");

Answer: FirstGuessFourthFinally, it is a car for 1250The End - text4

Q13: The following JavaScript code has some errors. Identify the errors and rewrite the code correctly.var my Value;if (myValue = 0window.write("OK");

var myValue;myValue = window.prompt('Please enter the value','');myValue = parseFloat(myValue);

if (myValue == 0){document.write("OK")};

Q14: The following JavaScript code has some errors and a missing statement. Identify the errors and rewrite the code correctly.var mark;bar bonus = 2;mark = window.pompt('Insert the student mark','');mark = mark + bonus;if mark >= 50document.write('Passed);Elsedocument.write('Failed');

var mark;var bonus = 0;mark = window.prompt('Insert the student mark','');mark = parseFloat(mark);mark = mark + bonus;if (mark >= 50){document.write('Passed');}else{document.write('Failed');}

Q15: Trace out the following JavaScript code and state how many different paths it has, showing a possible output of each path and discussing briefly the purpose of the two if-else statements included.var num1, num2, oper, result;

num1 = parseFloat(window.prompt('Enter the 1st number:',''));num2 = parseFloat(window.prompt('Enter the 2st number:',''));

oper = window.prompt('Enter the operator: (+ or -)','');if((oper == '+') || (oper == '-')){if (oper == '+'){result = num1 + num2;document.write(num1 + oper + num2 + '=' + result);}else{result = num1 - num2;document.write(num1 + oper + num2 + '=' + result);}}else{document.write('Invalid operator');}

Answer Q15: The given code has 3 different paths.1. For invalid operator rather than + or -, it will printout Invalid operator message.2. For + operator, it will printout the output of adding the two variables num1 and num2. For example if the user entered 5 and 7. The output message would be 5 + 7 = 12.3. For operator, it will printout the output so subtracting the variable num2 from num1. For example if the user entered 10 and 3, the output message would be 10 -3 = 7.

Q16: Trace the following code then answer the questions below:var x, y;x = 10;y = 0;while(x >= 6){x = x - 2;y = y + x;}

a. How many times will the loop be executed?3 times.b. What are the values of x and y after the execution of the above code?x = 4, y =18c. What do you expect to happen if the initial value of x was 5 instead of 10?If the initial value of x was 5 instead of 10, the condition (x >= 6) will evaluate false and the loop body will not evaluated at all. Hence, the values of x and y remain the same.Q17: Trace the following code then answer the questions below:var x, y;x = 1; y = 1;while (x != 7){y = y * x;x = x + 2;}

a. How many times will the loop be executed?3 times.b. What are the values of x and y after the execution of the above code?x = 7, y = 15c. What do you expect to happen if the initial value of x was 2 instead of 1?If the initial value of x was 2 instead of 1 it will be an infinite loop. This is because the value of x will be added by resulting 2, 4, 6, 8, 10, and thus the condition (x != 7) will never be false.

Q18: Given the following code: (M150A final exam, Fall 2009)for(var i = 0; i < 10; i++){document.write(i);}

a. What is the output?0123456789b. How would you modify the code so that it prints the numbers backwards?for (i = 9; i >= 0; i--){document.write(i);}

c. How would you modify the code so that it prints only the even numbers?for (i = 0; i < 10; i++){if (i % 2 == 0)document.write(i);}

or

for (i = 0; i < 10; i = i + 2){document.write(i);}

d. How would you modify the code so that it sums all the values of i and prints the final sum?var sum;sum = 0;for (i = 0; i < 10; i++){sum = sum + i;}document.write("final sum is " + sum);

Q19: Write a JavaScript program to print a table of consecutive even numbers from 2 to 30 and their square values.A sample output of this is as follows:24

416

636

28784

30900

for(var i = 2; i = 0; count = count - 1){document.write(string1.charAt(count));

}

Q26: Write a JavaScript code that takes a string from the user and outputs the number of vowels in that string. (M150A final exam, Fall 2009)[vowels are the letter a, e, i, o, u]var myString, counter = 0, ch;myString = window.prompt('Please enter a string','');for (i = 0; i < myString.length; i = i + 1){ch = myString.charAt(i);if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' ||ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U'){counter = counter + 1;}}document.write('The are ' + counter + ' vowels in the string');

Q27: What is the value of y after the execution of the following java script code?You are required to show how you have obtained the result. (M150A final exam, Fall 2009)var x = 4;var y = x + 2;var count = 1;while (count < x){y = y * 2;count = count + 1;}

Answer: 48

Q28: Write JavaScript code to calculate and print the area of a triangle as follows: Ask the user to enter the base and the height of the triangle. Calculate the area of the triangle based on the formula:area = 1 / 2 * (base * height) Print the calculated area in the output window.var base, height, area;base = window.prompt('Please enter the triangle base','');base = parseFloat(base);height = window.prompt('Please enter the triangle height','');height = parseFloat(height);area = 1 / 2 * (base * height);document.write('The triangle area is ' + area)

Q29: Using the while loop, write JavaScript code to do the following:a. Print the numbers from 1 to 20.var i = 1;while (i = 1){document.write(i);i = i - 1;}

c. Modify the code so that it prints only the even number from 1 to 20.var i = 2;while (i max){max = anArray[i];}}return max;};function calcMin(anArray){var min = anArray[0];for( i = 1; i < (anArray.length); i = i + 1){if(anArray[i] < min){min = anArray[i];}}return min;};function calcPositive(anArray){var positives = 0;for(i = 0; i < (anArray.length);i = i + 1){if(anArray[i] > 0){positives = positives + 1;}}return positives;};function calcNegative(anArray){var negatives = 0;for(i = 0; i < (anArray.length); i = i + 1){if(anArray[i] < 0){negatives = negatives + 1;}}return negatives;};function calcZero(anArray){var zeroes = 0;for(i = 0; i < (anArray.length); i = i + 1){if(anArray[i] == 0){zeroes = zeroes + 1;}}return zeroes;};function displayInfo(anArray){document.write('myArray has the following elements: ' + anArray + '
');document.write('The max value = ' + calcMax(anArray) + '
');document.write('The min value = ' + calcMin(anArray) + '
');document.write('The number of positive values = ' + calcPositive(anArray) + '
');document.write('The number of negative values = ' + calcNegative(anArray) + '
');document.write('The number of zero values = ' + calcZero(anArray) + '
');};var myArray = [2, 0, 1, -4, 10, 3, -2, 0, 3, 5];displayInfo(myArray);

Q36:

var ourNumber, userNumber, count;ourNumber = 501;userNumber = window.prompt('Enter a number between 1 and 1000', '');userNumber = parseFloat(userNumber);count = 1;while ( ourNumber != userNumber){if (ourNumber < userNumber){window.alert('You are above the right number!');}else{window.alert('You are below the right number!');}userNumber = window.prompt('Try again :', '');userNumber = parseFloat(userNumber);count = count + 1;}document.write('You guessed the number after ' + count + ' tries!');