Javascript - Domain Fundamentals Assignments
Javascript - Domain Fundamentals Assignments
1. Accept a char input from the user and display it on the console.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>Char io</title>
</head>
<body>
<form method="post" id="form">
<input type="text" id="char">
<button type="submit">Click me</button>
</form>
<script>
const char = document.getElementById('char');
$('#form').submit((e)=>{
console.log(char.value);
e.preventDefault()
})
</script>
</body>
</html>
2. Accept two inputs from the user and output their sum.
Number 1 Integer
Number 2 Float
Sum Float
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>Char io</title>
</head>
<body>
<form method="post" id="form">
<input type="number" id="num1"><br>
<input type="text" id="num2"><br>
<button type="submit">Click me</button><br>
<input type="text" value="Sum is:" id="sum" disabled><br>
</form>
<script>
const num1 = document.getElementById('num1');
const num2 = document.getElementById('num2');
const sum = document.getElementById('sum');
$('#form').submit((e)=>{
sum.value="Sum is:"+""
sum.value=sum.value+(Number(num1.value)+Number(num2.value));
e.preventDefault()
})
</script>
</body>
</html>
3. Write a program to find the simple interest.
a. Program should accept 3 inputs from the user and calculate simple interest for
the given inputs. Formula: SI=(P*R*n)/100)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>Si</title>
</head>
<body>
<form method="post" id="form">
<Label>Principle Amount</Label><br>
<input type="text" id="P" placeholder="Enter Principle Amount..."><br>
<Label>No of years</Label><br>
<input type="text" id="n" placeholder="Enter no. of years..."><br>
<Label>Rate of Interest</Label><br>
<input type="text" id="r" placeholder="Enter rate of Interest..."><br>
<button type="submit">Click me</button><br>
<input type="text" value="Simple Interest is:" id="si" disabled><br>
</form>
<script>
const P = document.getElementById('P');
const n = document.getElementById('n');
const r = document.getElementById('r');
const si = document.getElementById('si');
$('#form').submit((e)=>{
si.value="Simple Interest is:"+""
si.value=si.value+(Number(P.value)*Number(n.value)*Number(r.value))/100;
e.preventDefault()
})
</script>
</body>
</html>
4. Write a program to check whether a student has passed or failed in a subject after he
or she enters their mark (pass mark for a subject is 50 out of 100).
a. Program should accept an input from the user and output a message as
“Passed” or “Failed”
mark float
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>Result</title>
</head>
<body>
<form method="post" id="form">
<label for="">Enter your mark</label><br>
<input type="text" id="mark"><label for="" id="error"></label><br>
<button type="submit">Click me</button><br>
<input type="text" value="Result is:" id="result" disabled><br>
</form>
<script>
const mark = document.getElementById('mark');
const result = document.getElementById('result');
const error = document.getElementById('error')
$('#form').submit((e) => {
error.innerText = ""
result.value = "Result is:" + ""
if (Number(mark.value) > 100) {
error.innerText = "Enter score less than 100"
}
else {
if (Number(mark.value) >= 50) {
result.value = result.value + "Passed";
}
else {
result.value = result.value + "failed";
}
}
e.preventDefault()
})
</script>
</body>
</html>
5. Write a program to show the grade obtained by a student after he/she enters their
total mark percentage.
a. Program should accept an input from the user and display their grade as
follows
Mark Grade
> 90 A
80-89 B
70-79 C
60-69 D
50-59 E
< 50 Failed
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>Grade</title>
</head>
<body>
<form method="post" id="form">
<label for="">Enter your mark in percentage</label><br>
<input type="text" id="mark"><label for="" id="error"></label><br>
<button type="submit">Click me</button><br>
<input type="text" value="Grade is:" id="grade" disabled><br>
</form>
<script>
const mark = document.getElementById('mark');
const grade = document.getElementById('grade');
const error = document.getElementById('error')
$('#form').submit((e) => {
error.innerText = ""
grade.value = "Grade is:" + ""
if (Number(mark.value) > 100) {
error.innerText = "Enter value less than 100"
}
else {
if (Number(mark.value) >= 50 && Number(mark.value) < 60) {
grade.value = grade.value + "E";
}
else if(Number(mark.value) >= 60 && Number(mark.value) < 70){
grade.value = grade.value + "D";
}
else if(Number(mark.value) >= 70 && Number(mark.value) < 80){
grade.value = grade.value + "C";
}
else if(Number(mark.value) >= 80 && Number(mark.value) < 90){
grade.value = grade.value + "B";
}
else if(Number(mark.value) >= 90){
grade.value = grade.value + "A";
}
else {
grade.value = grade.value + "failed";
}
}
e.preventDefault()
})
</script>
</body>
</html>
6. Using the ‘switch case’ write a program to accept an input number from the user and
output the day as follows.
Input Output
1 Sunday
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
6 Friday
7 Saturday
<!DOCTYPE html>
<html lang="en">
<head>
<meta noset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>No io</title>
</head>
<body>
<form method="post" id="form">
<label for="">Enter week no.</label><br>
<input type="number" id="no"><label for="" id="error"></label><br>
<button type="submit">Click me</button><br>
<input type="text" value="Today is " id="week" disabled>
</form>
<script>
const no = document.getElementById('no');
const week = document.getElementById('week');
const error= document.getElementById('error');
$('#form').submit((e)=>{
week.value="Today is ";
error.innerText=""
switch(Number(no.value))
{
case 1:week.value+="Sunday"
break
case 2:week.value+="Monday"
break
case 3:week.value+="Tuesday"
break
case 4:week.value+="Wednesday"
break
case 5:week.value+="Thursday"
break
case 6:week.value+="Friday"
break
case 7:week.value+="Saturday"
break
default:error.innerText="Enter Valid input"
}
e.preventDefault()
})
</script>
</body>
</html>
a. Accept an input from the user and display its multiplication table
Eg:
Output:
1x5=5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50
<!DOCTYPE html>
<html lang="en">
<head>
<meta noset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>No io</title>
</head>
<body>
<form method="post" id="form">
<label for="">Enter a no.</label><br>
<input type="number" id="no"><br>
<button type="submit">Click me</button><br>
<span id="table"></span>
</form>
<script>
const no = document.getElementById('no');
const table = document.getElementById('table');
$('#form').submit((e)=>{
table.innerText="Table is ";
for(var i=1;i<=10;i++)
{
table.innerHTML+="<br>"+no.value+"x"+i+"="+Number(no.value)*Number(i);
}
e.preventDefault()
})
</script>
</body>
</html>
8. Write a program to find the sum of all the odd numbers for a given limit
a. Program should accept an input as limit from the user and display the sum
of all the odd numbers within that limit
Input: 10
<!DOCTYPE html>
<html lang="en">
<head>
<meta noset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>No io</title>
</head>
<body>
<form method="post" id="form">
<label for="">Enter a no.</label><br>
<input type="number" id="no"><br>
<button type="submit">Click me</button><br>
<span id="table"></span>
</form>
<script>
const no = document.getElementById('no');
const table = document.getElementById('table');
$('#form').submit((e)=>{
table.innerText="sum of odd no.s is ";
let sum=0;
let limit = Number(no.value)
for(let i=1;i<=limit;i++)
{
if (i%2!=0) {
sum+=i;
}
}
table.innerHTML+=sum;
e.preventDefault()
})
</script>
</body>
</html>
9. Write a program to print the following pattern (hint: use nested loop)
12
123
1234
12345
<!DOCTYPE html>
<html lang="en">
<head>
<meta noset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>No io</title>
</head>
<body>
<form method="post" id="form">
<label for="">Enter a no.</label><br>
<input type="number" id="no"><br>
<button type="submit">Click me</button><br>
<span id="table"></span>
</form>
<script>
const no = document.getElementById('no');
const table = document.getElementById('table');
$('#form').submit((e)=>{
}
table.innerHTML+="<br>"
e.preventDefault()
})
</script>
</body>
</html>
a. Program should accept an array from the user, swap the values of two arrays
and display it on the console
Input: 5
<!DOCTYPE html>
<html lang="en">
<head>
<meta noset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>Swap Array</title>
</head>
<body>
<form method="post" id="form">
<label for="">Enter a Limit of arrays</label><br>
<input type="number" id="no"><br>
<button type="submit">Click me</button><br>
</form>
<label for="" id="label1"></label>
<form action="" id="form2"></form>
<label for="" id="label2"></label>
<form action="" id="form3"></form>
<script>
const no = document.getElementById('no');
const table = document.getElementById('form2');
const label1 = document.getElementById('label1');
const label2 = document.getElementById('label2');
const form3 = document.getElementById('form3');
let array1=[]
let array2=[]
$('#form').submit((e)=>{
table.innerHTML+="<br>"+"<input
type="+"'number'"+"name="+"'array1'"+">"
}
table.innerHTML+="<br>"+"<button type="+"'submit'"+">Click me </button>"
e.preventDefault()
})
$('#form2').submit((e)=>{
let limit = Number(no.value)
e.preventDefault()
var k=[]
try{
let input = document.getElementsByName('array1');
for (var i = 0; i < input.length; i++) {
var a = input[i];
k = k + a.value +" " ;
}
console.log("first array is:"+k);
array1=k
label2.innerText="Enter "+limit+" elements one by one of array 2"
for(let i=1;i<=limit;i++)
{
form3.innerHTML+="<br>"+"<input
type="+"'number'"+"name="+"'array2'"+">"
}
form3.innerHTML+="<br>"+"<button type="+"'submit'"+">Click me </button>"
}
catch(err){
console.log(err);
}
})
$('#form3').submit((e)=>{
let limit = Number(no.value)
e.preventDefault()
var l=[]
try{
let input = document.getElementsByName('array2');
for (var i = 0; i < input.length; i++) {
var a = input[i];
l = l + a.value +" " ;
}
console.log("second array is:"+l);
array2=l
var temp=array1
array1=array2
array2=temp
console.log("first array after swapping is:"+array1+"and second array
is:"+array2);
}
catch(err){
console.log(err);
}
})
</script>
</body>
</html>
Input: 5
<!DOCTYPE html>
<html lang="en">
<head>
<meta noset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>Swap Array</title>
</head>
<body>
<form method="post" id="form">
<label for="">Enter a Limit of arrays</label><br>
<input type="number" id="no"><br>
<button type="submit">Click me</button><br>
</form>
<label for="" id="label1"></label>
<form action="" id="form2"></form>
<label for="" id="label2"></label>
<form action="" id="form3"></form>
<script>
const no = document.getElementById('no');
const table = document.getElementById('form2');
const label1 = document.getElementById('label1');
const label2 = document.getElementById('label2');
const form3 = document.getElementById('form3');
let array1=[]
let array2=[]
$('#form').submit((e)=>{
table.innerHTML+="<br>"+"<input
type="+"'number'"+"name="+"'array1'"+">"
}
table.innerHTML+="<br>"+"<button type="+"'submit'"+">Click me </button>"
e.preventDefault()
})
$('#form2').submit((e)=>{
let limit = Number(no.value)
e.preventDefault()
var k=[]
var count=0;
try{
let input = document.getElementsByName('array1');
for (var i = 0; i < input.length; i++) {
var a = input[i];
k = k + a.value;
}
array1=k
for (let index = 0; index < array1.length; index++) {
if (Number(array1[index])%2==0) {
count++
}
}
console.log("No of even no is: " +count);
}
catch(err){
console.log(err);
}
})
</script>
</body>
</html>
12. Write a program to sort an array in descending order
a. Program should accept and array, sort the array values in descending order
and display it
Input: 5
<!DOCTYPE html>
<html lang="en">
<head>
<meta noset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>Swap Array</title>
</head>
<body>
<form method="post" id="form">
<label for="">Enter a Limit of arrays</label><br>
<input type="number" id="no"><br>
<button type="submit">Click me</button><br>
</form>
<label for="" id="label1"></label>
<form action="" id="form2"></form>
<label for="" id="label2"></label>
<form action="" id="form3"></form>
<script>
const no = document.getElementById('no');
const table = document.getElementById('form2');
const label1 = document.getElementById('label1');
const label2 = document.getElementById('label2');
const form3 = document.getElementById('form3');
let array = []
let array2 = []
$('#form').submit((e) => {
}
table.innerHTML += "<br>" + "<button type=" + "'submit'" + ">Click me
</button>"
e.preventDefault()
})
$('#form2').submit((e) => {
let limit = Number(no.value)
e.preventDefault()
var k = []
var temp;
try {
let input = document.getElementsByName('array1');
for (var i = 0; i < input.length; i++) {
var a = input[i];
k.push(a.value);
}
if (k[i]<k[j]) {
temp=k[i]
k[i]=k[j];
k[j]=temp;
}
}
</script>
</body>
</html>
13. Write a program to identify whether a string is a palindrome or not
Input: MALAYALAM
Input: HELLO
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="./ajax.js"></script>
<title>Palindrome</title>
</head>
<body>
<form action="" id="form">
<input type="text" id="string">
<button type="submit">Click me!</button>
</form>
<script>
let temp=0;
const string = document.getElementById('string');
$('#form').submit((e) => {
e.preventDefault();
value = string.value
for (let i = 0; i < value.length - 1; i++) {
if (value[i] != value[value.length - i - 1]) {
temp = 1;
break;
}
}
if (temp!=0) {
console.log('string is not palindrome');
}
else{
console.log('string is palindrome');
}
})
</script>
</body>
</html>
Input: 3
Input:
123
456
789
Input:
10 20 30
40 50 60
70 80 90
11 22 33
44 55 66
77 88 99
<!DOCTYPE html>
<html lang="en">
<head>
<meta noset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>Swap Array</title>
</head>
<body>
<form method="post" id="form">
<label for="">Enter a Limit of arrays</label><br>
<input type="number" id="no"><br>
<button type="submit">Click me</button><br>
</form>
<label for="" id="label1"></label>
<form action="" id="form2"></form>
<script>
const no = document.getElementById('no');
const table = document.getElementById('form2');
const label1 = document.getElementById('label1');
const label3 = document.getElementById('label3');
const form3 = document.getElementById('form3');
let array = []
let array2 = []
$('#form').submit((e) => {
e.preventDefault()
let limit = Number(no.value)
label1.innerText = "Enter " + no.value * no.value + " Elements"
})
$('#form2').submit((e) => {
let limit = Number(no.value)
e.preventDefault()
var k = []
try {
let input = document.getElementById('array1');
value = input.value
k.push(input.value.split(","));
let count = 0
let a = [];
let b = [];
let j = 0;
while (j < limit) {
a = []
for (let i = 0; i < limit; i++) {
a.push(Number(k[0][count]))
count++
}
b.push(a)
j++
}
console.log(b);
input = document.getElementById('array2');
value = input.value
var d = []
d.push(input.value.split(","));
count = 0
let c = [];
j = 0;
while (j < limit) {
a = []
for (let i = 0; i < limit; i++) {
a.push(Number(d[0][count]))
count++
}
c.push(a)
j++
}
console.log(c);
var f = []
for (let k = 0; k < limit; k++) {
for (let l = 0; l < limit; l++) {
f.push(b[k][l] + c[k][l])
}
}
console.log("sum of 2 arrays is " + f);
}
catch (err) {
console.log(err);
}
})
</script>
</body>
</html>
15. Write a program to accept an array and display it on the console using functions
main()
1. Declare an array
2. Call function getArray()
3. Call function displayArray()
getArray()
displayArray()
function displayArray() {
console.log(array);
}
16. Write a program to check whether a given number is prime or not
a. Program should accept an input from the user and display whether the
number is prime or not
Input: 7
}
}
else if(no==2){
console.log(no+" is a prime no.");
}
if(flag==1){
console.log(no+" is not a prime no.");
}
else{
console.log(no+" is a prime no.");
}
17. Write a menu driven program to do the basic mathematical operations such as
addition, subtraction, multiplication and division (hint: use if else ladder or switch)
Written test = 81
Lab exams = 68
Assignments = 92
Write a program to find the grade of a student during his academic year.
a. Program should accept the scores for written test, lab exams and
assignments
b. Output the grade of a student (using weighted average)
Eg:
Written test = 55
Lab exams = 73
Assignments = 87
Eg 1:
Enter the annual income
495000
Income tax amount = 24750.00
Eg 2:
Enter the annual income
500000
Income tax amount = 25000.00
var tax
20. Write a program to print the following pattern using for loop
1
2 3
4 5 6
7 8 9 10
<!DOCTYPE html>
<html lang="en">
<head>
<meta noset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<title>No io</title>
</head>
<body>
<form method="post" id="form">
<label for="">Enter a no.</label><br>
<input type="number" id="no"><br>
<button type="submit">Click me</button><br>
<span id="table"></span>
</form>
<script>
const no = document.getElementById('no');
const table = document.getElementById('table');
$('#form').submit((e)=>{
e.preventDefault()
})
</script>
</body>
</html>
21. Write a program to multiply the adjacent values of an array and store it in an
another array
Eg:
1 2 3 4 5
Output
2 6 12 20
}
console.log(arr);
for (let j = 0; j < limit-1; j++) {
arr[j]*=arr[j+1]
}
arr.pop()
console.log(arr);
main()
getArray()
getArray()
displayArray()
1 2
3 4
5 6
7 8
Output:
6 8
10 12
function getArray() {
var k = []
var a= []
}
k.push(a);
return k;
}
function addArray(array1,array2) {
var sum=[]
var a=[]
for (let i = 0; i < limit; i++) {
a=[]
for (let j = 0; j < limit; j++) {
a.push(array1[i][j]+array2[i][j])
}
sum.push(a)
}
return sum
}
function displayArray(arr) {
console.log(arr);
}
let limit = jin("Enter limit ")
console.log("Enter elements of 1st array");
let array1=getArray()
console.log("Enter elements of 2nd array");
let array2=getArray()
let sum=addArray(array1,array2)
displayArray(sum)
23. Write an object oriented program to store and display the values of a 2D array
main()
1. Declare an array
2. Call function getArray()
3. Call function displayArray()
getArray()
Eg:
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
class Addarray{
getArray(limit) {
var k = []
var a= []
for (let j = 0; j < limit; j++) {
a=[]
for (let i = 0; i < limit; i++) {
a.push(Number(jin("")));
}
k.push(a);
return k;
}
displayArray(arr) {
console.log(arr);
}
}
circle() {
square() {
rectangle() {
triangle() {
Class Area{
circle(){
square(){
rectangle() {
}
triangle() {
Eg 1:
1. Circle
2. Square
3. Rectangle
4. Triangle
Output
Eg 2:
1. Circle
2. Square
3. Rectangle
4. Triangle
Output
circle(){
console.log("1.Circle");
console.log("2.Square");
console.log("3.Rectangle");
console.log("4.Triangle");
let choice =jin("Enter your choice")
switch(Number(choice))
{
case 1:mc.circle();
break;
case 2:mc.square();
break;
case 3:mc.rectangle();
break;
case 4:mc.triangle();
break;
default:exit=true;
}
if (exit) {
break;
}
}
25. Write a Javascript program to display the status (I.e. display book name, author
name & reading status) of books. You are given an object library in the code's template. It
contains a list of books with the above mentioned properties.Your task is to display the
following:
var library = [
readingStatus: true
},
{
title: 'Steve Jobs',
readingStatus: true
},
readingStatus: false
];
break;
case 2:
if (library[1].readingStatus==false) {
console.log("You still need to read "+library[1].title+ " by " +library[1].author);
}
else{
console.log("Already read "+library[1].title+ " by " +library[1].author);
}
break;
case 3:
if (library[2].readingStatus==false) {
console.log("You still need to read "+library[2].title+ " by " +library[2].author);
}
else{
console.log("Already read "+library[2].title+ " by " +library[2].author);
}
break;
default: console.log("Invalid input");
break;
}
26. Given a variable named my_string, try reversing the string using
my_string.split().reverse().join() and then print the reversed string to the console. If the try
clause has an error, print the error message to the console. Finally, print the typeof of the
my_string variable to the console.
Output format:
Error : ${err.message}
Eg:
a) Sample Input 0
"1234"
Sample Output 0
b) Sample Input 1
Number(1234)
Sample Output 1
Eg:
a) Sample Input 0
seven
Sample Output 0
notANumberError
b) Sample Input 1
77
Sample Output 1
hugeHeightError
c) Sample Input 2
0
Sample Output 2
tinyHeightError
d) Sample Input 3
Sample Output 3
try{
height=Number(height)
if (isNaN(height)) {
throw new notANumberError()
}
}
if(height <8 ){
throw new TinyHeight()
}
if(height >8 ){
throw new HugeHeightError()
}
if(height==8){
console.log(height);
}
}catch(customError)
{
console.log(customError);
}
28. Create a constructor function that satisfies the following conditions:
29. Write a myFilter function that takes 2 parameters: myArray and callback. Here,
myArray is an array of numbers and callback is a function that takes the elements of
myArray as its parameter and returns a boolean true if the sum of the number is even or
false if the sum of the number is odd.
a) Sample Input
12345
b) Sample Output
15
false
let myArray=[1,2,3,4]
let f
MyFilter(myArray,evenOrOdd)
function evenOrOdd(myArray) {
let sum=0;
let flag=false
for (let i = 0; i < myArray.length; i++) {
sum+=Number(myArray[i])
}
if (sum%2==0) {
flag=true
}
console.log(sum);
return flag
}
function MyFilter(array,evenOrOdd)
{
f=evenOrOdd(array)
console.log("even "+f);
}