0% found this document useful (0 votes)
1K views10 pages

JavaScript Assignments

1. The document provides instructions for several JavaScript assignments covering topics like variables, data types, operators, conditional statements, arrays and loops. 2. The assignments include prompts to write code that greets a user, displays messages, performs math operations, checks conditions, and iterates through arrays. 3. Later assignments involve more complex tasks like nesting loops, searching arrays, finding min/max values, and generating patterns from user input.

Uploaded by

Shahid Mahmood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
1K views10 pages

JavaScript Assignments

1. The document provides instructions for several JavaScript assignments covering topics like variables, data types, operators, conditional statements, arrays and loops. 2. The assignments include prompts to write code that greets a user, displays messages, performs math operations, checks conditions, and iterates through arrays. 3. Later assignments involve more complex tasks like nesting loops, searching arrays, finding min/max values, and generating patterns from user input.

Uploaded by

Shahid Mahmood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

JavaScript Assignments

Alert
1. Write a script to greet your website visitor using JS alert box.
2. Write a script to display following message on your web page:
(Hint : Use line break \n )

3. Generate the alert message through browser’s developer


Console.

Variables for Strings


1. Write script to
a) Declare a JS variable, titled message.
b) Assign “Hello World” to variable message
c) Display the message in alert box.

2. Declare a variable called book & give it the value “A smarter way to learn
JavaScript”. Display this in browser through JS.
(Hint: use document.write())

Variables for Number


1. Declare a variable called age & assign it your age. Show
your age in an alert box.
2. Declare a variable called birthYear & assign to it your
birth year. Display this in browser through JS.
(Hint: use document.write())

Legal and Illegal Variables


Display this in your browser.
a) A heading stating “Rules for naming JS variables”
b) Variable names can only contain ______, ______,
______ and ______.
For example: $my_1stVariable
c) Variables must begin with a ______, ______ or
_____.
For example: $name, _name or name
d) Variable names are case _________
e) Variable names should not be JS _________

Math Expressions: familiar operators


Do the following using JS Mathematic Expressions.
a. Declare a variable.
b. Show the value of variable in your browser like “Value
after variable declaration is: ?”.
c. Initialize the variable with some number.
d. Show the value of variable in your browser like “Initial
value: any number”.
e. Increment the variable.
f. Show the value of variable in your browser like “Value
after increment is: ?”.
g. Add 7 to the variable.
h. Show the value of variable in your browser like “Value
after addition is: ?”.
i. Decrement the variable.
j. Show the value of variable in your browser like “Value
after decrement is: ?”.
k. Show the remainder after dividing the variable’s value
by 3.
l. Output : “The remainder is : ?”.

Math Expressions: unfamiliar operators


What will be the output in variables a, b & result after execution of the
following script:

var a = 2, b = 1;
var result = --a - --b + ++b + b--;

Break it on each step:


--a;
--a - --b;
--a - --b + ++b;
--a - --b + ++b + b--;
Math Expressions: eliminating ambiguity
The Temperature Converter: It’s hot out! Let’s make a converter based on
the steps here.
a. Store a Celsius temperature into a variable.
b. Convert it to Fahrenheit & output “NN o C is NN o F”.
c. Now store a Fahrenheit temperature into a variable.
d. Convert it to Celsius & output “NN o F is NN o C”.

Conversion Formulae:

Prompts
1. Take user name form user on prompt and greet user by his name on
the alert box.

if statements
1. Write a program to take “gender” as input from user. If the user is
male, give the message: Good Morning Sir. If the user is female, give
the message: Good Morning Ma’am.

2. Write a program that takes a character (i.e. string of


length 1) and returns true if it is a vowel.

if...else and else if statements


1. Write a program to take input color of road traffic signal
from the user & show the message according to this:

Red = Must stop


Yellow = Ready to move
Green = Move now

Comparison Operators
1. Write a program that takes input a number from user
& state whether the number is positive, negative or
zero.
2. Write a program that
a. Store correct password in a JS variable.
b. Asks user to enter his/her password
c. Validate the two passwords:
i. Check if user has entered password. If not, then
give message “ Please enter your password”
ii. Check if both passwords are same. If they are
same, show message “Correct! The password
you entered matches the original password”.
Show “Incorrect password” otherwise.

Testing sets of conditions


1. Write a program that takes time as input from user in
24 hours clock format like: 1900 = 7pm. Implement
the following case using if, else & else if statements.

Arrays
1. Write a program to store 3 student names in an array.Take
another array to store score of these three students.
Assume that total marks are 500 for each student, display
the scores & percentages of students like.

Arrays: adding and removing elements


Arrays: removing, inserting, and extracting elements
1. Initialize an array with color names. Display the array elements in
your browser.
a. Ask the user what color he/she wants to add to the beginning &
add that color to the beginning of the array. Display the updated array
in your browser.
b. Ask the user what color he/she wants to add to the end & add that
color to the end of the array. Display the updated array in your
browser.
c. Add two more color to the beginning of the array. Display the
updated array in your browser.
d. Delete the first color in the array. Display the updated array in your
browser.
e. Delete the last color in the array. Display the updated array in your
browser.
f. Ask the user at which index he/she wants to add a color & color
name. Then add the color to desired position/index. . Display the
updated array in your browser.
g. Ask the user at which index he/she wants to delete color(s) & how
many colors he/she wants to delete. Then remove the same number
of color(s) from user-defined position/index. . Display the updated
array in your browser.

2. Write a program to initialize an array with city names. Copy 3 array


elements from cities array to selectedCities array.
3. Declare and initialize a multidimensional array representing the cities
of different countries.
(Array of arrays)
Hint: [[],[],[]]

for loops

1. Write a program to print multiplication table of any number using for


loop. Table number & length should be taken as an input from user.

2. Generate the following series in your browser. See example output.


a. Counting: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
b. Reverse counting: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
c. Even: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20
d. Odd: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19
e. Series: 2k, 4k, 6k, 8k, 10k, 12k, 14k, 16k, 18k, 20k

3. Write a program that prints fruits name from start of the array to
desired stop value. Given array:
var fruits = [apple, orange, banana, graps, mango];
(Hint: take stop value from user)
E.g. if user gives 3 as input value print apple, orange, banana.

for loops: flags, Booleans, array length, and breaks


1. You have an array
[“cake”, “apple pie”, “cookie”, “chips”, “patties”]
Write a program to enable “search by user input” in an array. After
searching, prompt the user whether the given item is found in the list
or not.
for loops nested
1. Write a program to identify the largest number and smallest number
in the given array. [24, 53, 78, 91, 12]

Challenge
Note: Challenge is not included in Assignments.

1. Write a program that will write out a wedge of stars. The user will
enter the initial number of stars, and the program will write out lines of
stars where each line has one few star than the previous line. Initial
number of stars: 7

Hints: i) use decrement


ii) use document.write
iii) use br tag in strings in document.write for line break

Output:
*******
******
*****
****
***
**
*

2. Write a program to create the following patterns in your browser.


Take number of lines as an input.
a.
*****
*****
*****
*****
b.
*
**
***
****
*****

c.
*****
****
***
**
*
d.
*
**
***
****
*****
******
*******
********
*********
**********
***********
**********
*********
********
*******
******
*****
****
***
**
*

You might also like