0% found this document useful (0 votes)
69 views

Suffyan WEB Assignment 2

The document contains answers to 14 questions about JavaScript programming. It includes code snippets demonstrating concepts like concatenating strings, using functions, prompts, alerts, and changing element styles. The last question involves calculating the total cost of paint buckets, validating user input, and changing the font color based on the selected paint color.

Uploaded by

Suffyan Arshad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Suffyan WEB Assignment 2

The document contains answers to 14 questions about JavaScript programming. It includes code snippets demonstrating concepts like concatenating strings, using functions, prompts, alerts, and changing element styles. The last question involves calculating the total cost of paint buckets, validating user input, and changing the font color based on the selected paint color.

Uploaded by

Suffyan Arshad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

University Of Gujrat

Department of Computer Science


Introduction to web programing
Assignment # 2
Suffyan Arshad
19010819-027
MS.c-CS-A
Submitted to: Mr. Zaheer Ahmad.
Question 1: Find the errors in the following script:
<html>

<head>

<title>Finding Errors</title>

</head>

<body bgcolor="yellow" text="blue">

<script type="text/javascript"

document.writeln("Two, ")

document.writeln ("Three, ")

document.write('Blast off....<br />");

</script>

</body>

</html>

Answer:
<html>

<head>

<title>Finding Errors</title>

</head>

<body bgcolor="yellow" text="blue">

<script type="text/javascript"> //closing bracket was missing//

document.write("Two, ") // function is document.write not document.writein//

document.write ("Three, ") // function is document.write not document.writein//

document.write(“Blast off....<br />"); //we should be use double quotes//

</script>

</body>
</html>

Question 2: How would you concatenate the following three strings with
JavaScript

"trans" "por" "tation" .

Answer:
<html>

<head>

<title>Concatenation</title>

<script type="text/javascript">

function concate()

var a="trans";

var b="por";

var c="tation";

var d=a+b+c;

alert("Value after concatenation : "+d);

</script>

</head>

<body>

<p>Before concatenation strings are:</p>


<p>trans</p>

<p>por</p>

<p>tation</p>

<button type="button" onClick="concate();">Click here for cancatenation</button>

</body>

</html>

Question 3 : Write a script that demonstrates how concatenation works.

Answer :

<html>

<head>

<title> Concatenation </title>

</head>

<body>

<script type="text/javascript">

var a="aslam";

var b="ali";

var c="bai";

document.write (a+b+c);

</script>

</body>
</html>

Question.4. Create a JavaScript program that will print “Hello, world! Isn’t
life great?” in an Arial bold font, size 14, and make the background color of
the screen light green.

Answer:
<html>

<head>

<title> question number 4 </title>

</head>

<body id=”back”>

<p id=”p”><b> Hello, world! Isn’t life great?</b> </p>

<script>

var a=document.getElementById(“back”);

a.style.background=”lightgreen”;

var b=document.getElementById(“p”);

b.style.fontFamily=”Arial”;

b.style.fontSize=”14px”;

</script>

</body>

</html>
Question 5: Add two strings to the first JavaScript program—your first name
and last name—concatenated and printed in a blue sans serif font, size 12.

Answer:
<html>

<head>

<title>question 5</title>

</head>

<body text="blue" style="font-size:12; font-family: sans-serif">

<script>

var a="suffyan";

document.write("First Name: "+a+"<br/>");

var b="Arshad";

document.write("Second Name: "+b+"<br/>");

var c=a+" "+b;

document.write("Full Name: "+c+"<br/>");

</script>

</body>

</html>

Question 6. In the Location field of your browser, test the value of an


expression using the JavaScript: protocol.

Answer:
<html>

<head>

<title>question 6</title>

<script>
function op(a,b,c)

return X+Y+Z/5;

</script>

</head>

<body>

<script>

var a=prompt(“Enter value of X= ”);

var b=prompt(“Enter value of Y =”);

var c=prompt(“Enter value of Z=”);

var m=op(a,b,c);

document.write(“RESULT =”+ m);

</script>

</body>

</html>

Question 7: What is wrong with the following alert box?

Alert(“hello”,”world!”);

Answer : The word (hello) is just shown in the alert box .Because syntax of alert box just
exceute just first doble quotes.

Question 8: Create a JavaScript program that produces the previous alert


box. When the alert dialog box appears, what does the program do?

Answer :
<html>

<title>question 8</title>
<head>

<script>

function f()

alert("ASLAM U ALIKUM");

alert(" do you hear me");

</script>

</head>

<body>

<form>

<h3>Please click on button</h3>

<input type="button" name="btn1" value="Click here" onClick="f();">

</form>

</body>

</html>

Question 9: Write a script that contains four variables in the head of the
document: The first one contains your name, the second contains the value 0,
the third one is declared but has no value, and the last contains an empty
string. In the body of the document, write another script to display the type of
each (use the typeof operator).

Answer:
<html>

<head>

<title> question 9 </title>


<script>

var a=”name”

var b=0;

var c;

var d=” ”;

</script>

</head>

<body>

<script>

document.write(typeof a);

document.write(typeof b);

document.write(typeof c)

document.write(typeof d);

</script>

</body>

</html>

Question 10: What is the return value of the prompt method if the user
doesn’t enter anything? Where is the return value stored?

Answer: If user doesn’t enter anything and press ok button then prompt return nothing. If
prompt show highlighted value in the prompt and user doesn’t enter anything and press ok
button then prompt return highlighted text. If user doesn’t enter anything or enter some value in
prompt and press cancel button then prompt return null value.The return value of prompt stored
in the variable for which we use prompt.

Question 11: Create a JavaScript program that prompts the user for a phone number and
then asks the user for confirmation.

Answer:

<html>
<head>

<title> question 11 </title>

</head>

<body>

<script>

var no=prompt(“Enter your phone number”)

var con=confirm(“confirm your phone number”);

if(con==true)

document.write(“ your phone number is= :”+ no)

else

document.write(“you do not confirm phone number”)

</script>

</body>

</html>

Question 12: Write a function that will calculate and return the amount that
should be paid as a tip for a restaurant bill. The tip will be 20 percent of the
total bill.

Answer:
<html>

<head>

<title> question 12 </title>


<script>

function cal( t)

var tp=20/100*a;

return tp;

</script>

</head>

<body>

<script>

var t=prompt(“Enter total bill”);

var c=cal( t);

document.write(“The tip is ”+c);

</script>

</body>

</html>

Question 13: Create a function called changeColor () that will be called when
the user presses one of two buttons. The first button will contain the text
“Press here for a yellow background”. The second button will contain the text
“Press here for a light green background”. The function will take one
parameter, a color. Its function is to change the background color of the
current document; for example, bgColor=“yellow”.

Answer:
<html >

<head>

<script>
function changeColor(a)

document.body.style.background=a;

</script>

</head>

<body>

<button type="button" onclick="changeColor('yellow')">click for a yellow


background</button>

<button type="button" onclick="changeColor('lightgreen')">click for a light green


background</button>

</body>

</html>

Question 14: Write a function that returns the total cost of any number of
buckets of paint. Ask the user how many buckets he or she is going to buy and
for the cost of one bucket. Ask the user the color of the paint. Calculate and
return what he or she owes. Change the color of the font to the color of the
paint. Use the catch/try/throw statements to check for invalid input if the user
doesn’t give you a valid value for the number of buckets, such as a number
less than 0, or gives you no input, or types in a string, and so on, check that
the user doesn’t give you a color of paint that is the same color as the
background of your document or you will not be able to see the color of the
font.

Answer:
<html >

<head>

</head>
<body>

<button type="button" onclick="TotalAmount(noofbucket,price,color)">calculate also set the


color</button>

<script>

var p=prompt("Enter the price","");

var b=prompt("Enter the number of bucket","");

var c=prompt("Enter your color","suppose yellow");

function Total(b,p,c)

totalcost=b*p;

document.write("the amount is ",totalcost,"</br>");

var str = new String("same as the color you enter is dialog ");

document.write(str.fontcolor(color));

try {

if(x == "") throw "epmty";

if(isNaN(x)) throw "not a number";

x = Number(x);

if(x < 0) throw "too low";

catch(err) {

message.innerHTML = "Input is " + err;

</script>

</body>
</html>

You might also like