Q1.
Write a function that takes two numbers and returns their sum.
function addTwoNumbers(a, b)
return a + b;
[Link](addTwoNumbers(5, 7));
Q2.
Write a function that takes an array of numbers and returns the
largest number.
let arr = [3, 7 , 8 , 5 , 4];
let max = arr[0];
for(let i = 1; i < [Link]; i++){
if (arr[i]> max){
max = arr[i];
}
}
Q3.
Write a script that checks if a number is even or odd. function
checkEvenOrOdd(number) {
if (number % 2 === 0) {
[Link](number + ' is even.');
else
[Link](number + ' is odd.');
checkEvenOrOdd(9);
checkEvenOrOdd(8);
Q4.
Write a loop that prints numbers from 100 to 90.
for (let i = 100; i >= 90; i--) {
[Link](i);
Q5.
Create an object representing a book with properties like title,
author, year Published, and genre.
const book = {
title: "The Great Gatsby",
author: "F. Scott Fitzgerald",
yearPublished: 1925,
genre: "Novel"
};
[Link](book);
};
Q7.
Demonstrate type conversion between different data types (e.g., string
to number, number to Boolean).
const str = "257";
const num = Number(str);
[Link](num);
const num2 = 0;
const bool = Boolean(num2);
[Link](bool);
Q8.
Create a class Person with properties for name and age.
class Person {
constructor(name, age) {
[Link] = name;
[Link] = age;
}
const person = new Person("Lakhan”,23);
[Link](person);
Q9.
Add a method greet that prints a greeting message.
class Person {
constructor(name, age) {
[Link] = name;
[Link] = age;
greet() {
[Link](`Hello, my name is ${[Link]} and I am
${[Link]} years old.`);
const person = new Person("Lakhan", 23);
[Link]();
Q10.
Create a subclass Student that extends Person and adds a property
for grade. Override the greet method to include the student's grade
in the message.
class Person {
constructor(name, age) {
[Link] = name;
[Link] = age;
greet() {
[Link](`Hello, my name is ${[Link]} and I am
${[Link]} years old.`);
class Student extends Person {
constructor(name, age, grade) {
super(name, age);
[Link] = grade;
greet() {
[Link](`Hello, my name is ${[Link]}, I am $
{[Link]} years old, and I am in grade $
{[Link]}.`);
const student = new Student("Lakhan", 21, "C");
[Link]();
Q11.
Write a function to handle a condition which throws an exception
function checkCondition(condition) {
if (!condition) {
throw new Error("Condition not met!");
}
[Link]("Condition met.");
try {
checkCondition(false);
} catch (e) {
[Link]([Link]);
Q12.
Create a function that returns a promise which resolves after 2
seconds with a success message.
function resolve () {
return new Promise((resolve) => {
setTimeout(() => {
resolve("Success!");
}, 2000);
});
}
resolve ().then(message => [Link](message));
Q13.
Chain [Link] method to log the success message and a .catch
method to handle any errors.
function resolve () {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Success!");
// reject("Failed!");
}, 2000);
});
resolve ()
.then(message => [Link](message))
.catch(error => [Link](error));
Q14.
Rewrite the above function (point 12) using async and await.
async function resolve () {
return new Promise((resolve) => {
setTimeout(() => {
resolve("Success!");
}, 2000);
});
async function run() {
try {
const message = await resolve ();
[Link](message);
} catch (error) {
[Link](error);
run();
Q15.
Create two files, [Link] and [Link].
In [Link], export functions for addition and subtraction.
In [Link], import these functions and use them to perform some
calculations
function add(a, b) {
return a + b;
function subtract(a, b) {
return a - b;
[Link] = { add, subtract };
// [Link]
const { add, subtract } = require('./math');
[Link](add(2, 5));
[Link](subtract(7, 5));
Q16.
Create a form with an input field and a submit button.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Form Example</title>
</head>
<body>
<form id="myForm">
<input type="text" id="myInput" >
<button type="submit">Submit</button>
</form>
</body>
</html>
Q17.
Write JavaScript to prevent the form from submitting if the input
field is empty and display an error message.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Form Example</title>
</head>
<body>
<form id="myForm">
<input type="text" id="myInput">
<button type="submit">Submit</button>
<p id="errorMessage" style="color:red;"></p>
</form>
<script>
[Link]('myForm').addEventListener('sub
mit', function(event) {
const inputField =
[Link]('myInput');
const errorMessage =
[Link]('errorMessage');
if ([Link]() === '') {
[Link] = 'Input field cannot be
empty!';
[Link]();
} else {
[Link] = '';
});
</script>
</body>
</html>