[go: up one dir, main page]

0% found this document useful (0 votes)
10 views7 pages

HWD Js Assignment 3

Uploaded by

chiranthyotu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

HWD Js Assignment 3

Uploaded by

chiranthyotu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

JAVA SCRIPT ASSIGNMENT

1->Write a JavaScript program to print your name, age, and city in


the console.
console.log("Name: Chiranth R Rao");

console.log("Age: 19");

console.log("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) {

console.log("Addition:", a + b);

console.log("Subtraction:", a - b);

console.log("Multiplication:", a * b);

console.log("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) console.log(n + " is Even");

else console.log(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++)

console.log(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:");

console.log(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:"));

console.log(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++) {

colours.push(prompt("Enter favourite colour " + (i+1) + ":"));

for (let c of colours) console.log(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:")

};

console.log("Name: " + person.name);

console.log("Age: " + person.age);

console.log("City: " + person.city);

Chiranth R Rao 7

You might also like