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

CS - JavaScript

Uploaded by

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

CS - JavaScript

Uploaded by

xynxgdpjhm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

A. True or false?

1. JavaScript is a very complex scripting language. False


2. The actions you can perform on or with objects are called methods. True
3. A program can have more than one <SCRIPT> ... </SCRIPT> tags. True
4. Single line comments end with a double-slash (//). True
5. JavaScript helps to add interactive elements to HTML pages. True

B. Give JavaScript statements to perform the following tasks.

1. Concatenating (joining) two strings: "Click" and "Start".


Ans:
<html>
<head>
<body>
<script language= "JavaScript">
var strl ,str2
strl="Click"
str2="Start"
document.write(strl +str2+ "</Br>");
document.write(strl +" "+str2);
</script>
</body>
</head>
</html>

2. Using single line and multiple line comments.


Ans:
Single line comments:
// This is comment line 1
Multiple line comments:
/* This is comment line 1
This is comment line 2 */

3. Declaring four variables: length, breadth, height and volume.


Ans: var length, breadth, height, volume
4. Display a confirm dialog box with the message "Plant more trees".
Ans:
<HTML>
<BODY>
<SCRIPT language=<"JavaScript">
var reply = confirm("Plant more trees")
document.write(reply);
</SCRIPT>
</BODY>
</HTML>

C. Describe the use of the following methods in JavaScript.

1. document.write()
The document.write() method is used for displaying the text on the browser window. It uses
an object called document, which refers to the current document on the browser window.

2. window.prompt()
The prompt dialog box is also a method of the window object. This method is used for
getting input from the user. It displays a Explorer User Prompt dialog box with a message, and
an input field.

3. window.alert()
window.alert () is a method of the window object. It is the simplest dialog box used to display a
short message to the user in a separate small window. The dialog box contains the text given as
a parameter to the alert( ) method and a button labelled OK.

4. window.confirm()
This method returns true if OK is pressed and false if Cancel is pressed.

5. parseInt()
parseInt() method converts a string value into integers (numbers without decimal places)

6. parseFloat()
parseFloat() method converts a string value into floating numbers (numbers with decimal
values).
D. Answer the following questions.

1. What is JavaScript? Why is it important?


Ans: JavaScript is a simple scripting language that adds interactivity to web pages, running
when changes are needed, like after page load or on user clicks. It requires constructs like `IF`
statements and `FOR` loops, unlike HTML. Key reasons to use JavaScript include:

- Adds interactivity to HTML.


- Lightweight and easy to use.
- Free to use, no license required.
- Supported by major browsers (e.g., Chrome, Firefox, Safari).
- Can be embedded directly in HTML.

2. Discuss object model in JavaScript.


Ans: JavaScript uses an Object Model, allowing you to structure programs around objects. In
programming, elements like the browser window, HTML document, and forms are objects, which
contain other objects like text boxes and radio buttons.

3. How can you use HTML tags in JavaScript?


Ans: JavaScript can include HTML tags to create elements displayed in the browser. All HTML
tags are valid, and when document.write() sends a string to the browser, it processes HTML
tags as code and plain text as simple text.

4. How do you declare variables in JavaScript?


Ans: You can declare a variable in JavaScript by using var along with the variable name. For
example, variables n1, fname and age are declared below. These variables are empty by
default with no value:
var n1, result
var fname
var age
However, you can also assign values to the variable at the time of declaring them or
later. For example,
var n1=2
var fname=“Avika”

5. Discuss various types of operators in JavaScript.


Ans: Various types of operators in JavaScript are discussed below.
a. Arithmetic operators: These operators are used to perform arithmetic calculations using
variables or constants.
b. Comparison operators: These operators are used to test if two variables relate to each other
in the specified way.
c. Logical operators: These operators perform logical operations on variables.
d. String operator: A string is a set of characters. To join two or more string values together, you
use the plus ‘+’ operator. This is also known as string concatenation operator.
Lab work

A. Write a JavaScript program to input marks of all your subjects and then calculate the
average.
Ans:
<html>
<body>
<script language="JavaScript">
var Myanmar = Number(prompt("Enter the marks of Myanmar"));
var English = Number(prompt("Enter the marks of English"));
var Mathematics = Number(prompt("Enter the marks of
Mathematics"));
var Physics = Number(prompt("Enter the marks of Physics"));
var Chemistry = Number(prompt("Enter the marks of
Chemistry"));
var Biology = Number(prompt("Enter the marks of Biology"));
var Computer_Science = Number(prompt("Enter the marks of
Computer Science"));
var Social_Studies = Number(prompt("Enter the marks of Social
Studies"));
var Art = Number(prompt("Enter the marks of Art"));

var average = (Myanmar + English + Mathematics + Physics +


Chemistry + Biology + Computer_Science + Social_Studies + Art) / 10;

alert("Your average marks are: " + average);


</script>
</body>
</html>
B. Write a JavaScript program to take user inputs of sides of a rectangle and calculate its
area.
Ans:
<html>
<body>
<script lang="Javascript">
var l, w, area
l=prompt("Enter the length of the rectangle:")
w=prompt("Enter the width of the rectangle:")
area=l*w
document.write("Your rectangle's area is:" +area)
</script>
</body>
</html>

C. Input the first name and last name from the user and display a message "The name
you entered is <first name><last name>" .
Ans:
<html>
<body>
<script lang="Javascript">
var firstname,lastname
firstname=prompt("Enter your first name")
lastname=prompt("Enter your last name")
document.write("The name you entered is, " + firstname + " " +
lastname + " ")
</script>
</body>
</html>

D. Input your school name and display it in red and in a bigger font size.
Ans:
<html>
<body>
<script lang="Javascript">
var schoolname = prompt("Enter your school name:");
document.write("<h1 style='color:red;'>Your school name is: " +
schoolname + "</h1>");
</script>
</body>
</html>

You might also like