JAVA SCRIPT ASSIGNMENT
1->Write a JavaScript program to print your name, age, and city in
the console.
[Link]("Name: Chiranth R Rao");
[Link]("Age: 19");
[Link]("City: Bengaluru");
2->Create a program that takes two numbers and prints the result
of addition, subtraction, multiplication, and division. DOM
Manipulation
function calculate(a, b) {
[Link]("Addition:", a + b);
[Link]("Subtraction:", a - b);
[Link]("Multiplication:", a * b);
[Link]("Division:", a / b);
calculate(10, 5);
Chiranth R Rao 1
JAVA SCRIPT ASSIGNMENT
3->Write a program to check if a number is even or odd using an if-
else statement.
let n = parseInt(prompt("Enter a number:"));
if (n % 2 === 0) [Link](n + " is Even");
else [Link](n + " is Odd");
Chiranth R Rao 2
JAVA SCRIPT ASSIGNMENT
4->Write a program to print all numbers from 1 to 10 using a for
loop.
for (let i=1; i<=10; i++)
[Link](i);
Chiranth R Rao 3
JAVA SCRIPT ASSIGNMENT
5->Write a function called greetUser(name) that takes a name and
returns a greeting message.
function greetUser(name) {
return "Hello, " + name + "!";
let user = prompt("Enter your name:");
[Link](greetUser(user));
Chiranth R Rao 4
JAVA SCRIPT ASSIGNMENT
6->Write a function isAdult(age) that returns true if age is 18 or
above, otherwise false. Test it with different ages.
function isAdult(age) {
return age >= 18;
let a = parseInt(prompt("Enter age:"));
[Link](isAdult(a) ? "Adult" : "Not Adult");
Chiranth R Rao 5
JAVA SCRIPT ASSIGNMENT
7->Create an array of your 5 favourite colours. Use a loop to print
each colour on a new line.
let colours = [];
for (let i=0; i<5; i++) {
[Link](prompt("Enter favourite colour " + (i+1) + ":"));
for (let c of colours) [Link](c);
Chiranth R Rao 6
JAVA SCRIPT ASSIGNMENT
8->Create a person object with name, age, and city. Write code to
access and print each property of the object.
let person = {
name: prompt("Enter name:"),
age: prompt("Enter age:"),
city: prompt("Enter city:")
};
[Link]("Name: " + [Link]);
[Link]("Age: " + [Link]);
[Link]("City: " + [Link]);
Chiranth R Rao 7