0% found this document useful (0 votes)
109 views

Javascript - Domain Fundamentals Assignments

The document contains 6 programming assignments involving accepting user input and performing basic operations or conditional checks on the input data. The assignments include: 1) Displaying a character input by the user. 2) Calculating and displaying the sum of two numbers input by the user. 3) Calculating simple interest given principal, rate, and time inputs. 4) Checking if a test score input passes or fails. 5) Displaying a grade based on a percentage mark input. 6) Displaying the day of the week corresponding to a number input using switch-case. HTML/JavaScript code snippets are provided as solutions for each assignment.

Uploaded by

VISHNU S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Javascript - Domain Fundamentals Assignments

The document contains 6 programming assignments involving accepting user input and performing basic operations or conditional checks on the input data. The assignments include: 1) Displaying a character input by the user. 2) Calculating and displaying the sum of two numbers input by the user. 3) Calculating simple interest given principal, rate, and time inputs. 4) Checking if a test score input passes or fails. 5) Displaying a grade based on a percentage mark input. 6) Displaying the day of the week corresponding to a number input using switch-case. HTML/JavaScript code snippets are provided as solutions for each assignment.

Uploaded by

VISHNU S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

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.

Variable Data Type

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)

Variable Data Type

Principal amount (P) Integer

Interest rate (R) Float

Number of years (n) Float

Simple Interest (SI) 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>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”

Variable Data type

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

Variable Data type

Total 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>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

Any other input Invalid Entry

<!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>

7. Write a program to print the multiplication table of given numbers.

a. Accept an input from the user and display its multiplication table

Eg:

Output: Enter a number


Input: 5

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

For example if the input limit is 10 then the result is 1+3+5+7+9 = 25

Output: Enter a limit

Input: 10

Output: Sum of odd numbers = 25

<!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)=>{

let limit = Number(no.value)


for(let i=1;i<=limit;i++)
{
for (let j = 1; j <=i; j++) {
table.innerHTML+=j+" ";

}
table.innerHTML+="<br>"

e.preventDefault()
})
</script>
</body>
</html>

10. Write a program to interchange the values of two arrays.

a. Program should accept an array from the user, swap the values of two arrays
and display it on the console

Eg: Output: Enter the size of arrays

Input: 5

Output: Enter the values of Array 1

Input: 10, 20, 30, 40, 50

Output: Enter the values of Array 2

Input: 15, 25, 35, 45, 55

Output: Arrays after swapping:

Array1: 15, 25, 35, 45, 55


Array2: 10, 20, 30, 40, 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>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)=>{

let limit = Number(no.value)


label1.innerText="Enter "+no.value+" Elements one by one of array 1"
for(let i=1;i<=limit;i++)
{

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>

11. Write a program to find the number of even numbers in an array


a. Program should accept an array and display the number of even numbers
contained in that array

Eg: Output: Enter the size of an array

Input: 5

Output: Enter the values of array

Input: 11, 20, 34, 50, 33

Output: Number of even numbers in the given array is 3

<!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)=>{

let limit = Number(no.value)


label1.innerText="Enter "+no.value+" Elements one by one of array 1"
for(let i=1;i<=limit;i++)
{

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

Eg: Output: Enter the size of an array

Input: 5

Output: Enter the values of array

Input: 20, 10, 50, 30, 40

Output: Sorted array:

50, 40, 30, 20, 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>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) => {

let limit = Number(no.value)


label1.innerText = "Enter " + no.value + " Elements one by one of array 1"
for (let i = 1; i <= limit; i++) {

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 temp;
try {
let input = document.getElementsByName('array1');
for (var i = 0; i < input.length; i++) {
var a = input[i];
k.push(a.value);
}

console.log("array before sorting "+k);


for (let i = 0; i < k.length-1; i++) {
for (let j = i+1; j <k.length; j++) {

if (k[i]<k[j]) {
temp=k[i]
k[i]=k[j];
k[j]=temp;

}
}

console.log("array after sorting "+k);


}
catch (err) {
console.log(err);
}
})

</script>
</body>

</html>
13. Write a program to identify whether a string is a palindrome or not

a. A string is a palindrome if it reads the same backward or forward eg:


MALAYALAM

Program should accept a string and display whether the string is a


palindrome or not

Eg: Output: Enter a string

Input: MALAYALAM

Output: Entered string is a palindrome

Eg 2: Output: Enter a string

Input: HELLO

Output: Entered string is not a palindrome


<!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="./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>

14. Write a program to add two two dimensional arrays

a. Program should accept two 2D arrays and display its sum

Eg: Output: Enter the size of arrays

Input: 3

Output: Enter the values of array 1

Input:

123
456

789

Output: Enter the values of array 2

Input:

10 20 30

40 50 60

70 80 90

Output: Sum of 2 arrays is:

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"

table.innerHTML += "<br>" + "Enter elements of matrix 1 row by row<input


type=" + "'text'" + "id=" + "'array1'" + " maxlength=" + `'${limit * limit * limit}'` +
"><br>"
table.innerHTML += "<br>" + "Enter elements of matrix 2 row by row<input
type=" + "'text'" + "id=" + "'array2'" + " maxlength=" + `'${limit * limit * limit}'` +
"><br>"
table.innerHTML += "<br>" + "<button type=" + "'submit'" + ">Click me
</button>"

})
$('#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

a. Program should contain 3 functions including main() function

main()

1. Declare an array
2. Call function getArray()
3. Call function displayArray()

getArray()

1. Get values to the array

displayArray()

1. Display the array values


import {question as jin} from 'readline-sync';
let a;
let b;
let array=getArray();
displayArray()
function getArray()
{
let array =[]
let limit = jin("Enter array limit: ")
console.log("Enter Array Elements: ");
for (let i = 0; i < limit; i++) {
array.push(jin(""))
}
return array
}

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

Eg: Output: Enter a number

Input: 7

Output: Entered number is a Prime number

import {question as jin} from 'readline-sync';


let no = jin("Enter a no. ")
let flag=0
if (no>2) {
for (let i = 2; i < no/2; i++) {
if (no%i==0) {
flag=1;
break
}

}
}
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)

a. Program should have 4 functions named addition(), subtraction(),


multiplication() and division()
b. Should create a class object and call the appropriate function as user prefers
in the main function

import {question as jin} from 'readline-sync'


class Calc{
add() {
console.log("Enter numbers: ");
let num1=jin("")
console.log("+");
let num2=jin("")
let sum=Number(num1)+Number(num2)
console.log("="+sum);
}
sub() {
console.log("Enter numbers: ");
let num1=jin("")
console.log("-");
let num2=jin("")
let sum=Number(num1)-Number(num2)
console.log("="+sum);
}
mult() {
console.log("Enter numbers: ");
let num1=jin("")
console.log("x");
let num2=jin("")
let sum=Number(num1)*Number(num2)
console.log("="+sum);
}
div() {
console.log("Enter numbers: ");
let num1=jin("")
console.log("/");
let num2=jin("")
let sum=Number(num1)/Number(num2)
console.log("="+sum);
}
}
const c1=new Calc()
console.log("1.Addition");
console.log("2.Substraction");
console.log("3.Multiplication");
console.log("4.Division");
let ch=jin("Enter your choice: ")
switch (Number(ch)) {
case 1:
c1.add()
break;
case 2:
c1.sub()
break;
case 3:
c1.mult()
break
case 4:
c1.div()
break;

default:console.error("Paranjathu vallo type cheyada");


break;
}
18. Grades are computed using a weighted average. Suppose that the written test
counts 70%, lab exams 20% and assignments 10%.

If Arun has a score of

Written test = 81

Lab exams = 68

Assignments = 92

Arun’s overall grade = (81x70)/100 + (68x20)/100 + (92x10)/100 = 79.5

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:

Enter the marks scored by the students

Written test = 55

Lab exams = 73

Assignments = 87

Grade of the student is 61.8

import { question as jin} from "readline-sync";


let wt=jin("Enter your score obtained in written test: ")
let lt=jin("Enter your score obtained in lab test: ")
let at=jin("Enter your score obtained in assignment: ")
let gp=Number(wt*70)/100 + Number(lt*20)/100 + Number(at*10)/100
console.log("Grade of the student is "+gp);

19. Income tax is calculated as per the following table

Annual Income Tax percentage

Up to 2.5 Lakhs No Tax

Above 2.5 Lakhs to 5 5%


Lakhs

Above 5 Lakhs to 10 20%


Lakhs

Above 10 Lakhs to 50 30%


Lakhs

Write a program to find out the income tax amount of a person.


a. Program should accept annual income of a person
Output the amount of tax he has to pay

Eg 1:
Enter the annual income
495000
Income tax amount = 24750.00

Eg 2:
Enter the annual income
500000
Income tax amount = 25000.00

import {question as jin} from "readline-sync"

var tax

let salary=Number(jin("Enter your salary in lakhs: "));

if(2.5<salary && salary<=5){


tax= 5;
}else if(salary>5&&salary<=10){
tax=20;
}else if(salary>10&&salary<=50){
tax=30;
}
console.log("Hmm Your tax amount will be "+(tax/100)*salary+"lakhs and tax
rate will be "+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)=>{

let limit = Number(no.value)


let star=1
for(let i=1;i<=limit;i++)
{
for (let j = 1; j <=i; j++) {
table.innerHTML+=star+"&nbsp;&nbsp;&nbsp;&nbsp;";
star++
}
table.innerHTML+="<br>"

e.preventDefault()

})
</script>
</body>
</html>
21. Write a program to multiply the adjacent values of an array and store it in an
another array

a. Program should accept an array


b. Multiply the adjacent values
c. Store the result into another array

Eg:

Enter the array limit

Enter the values of array

1 2 3 4 5

Output

2 6 12 20

import { question as jin } from "readline-sync";


let arr=[]
let limit=jin("Enter limit ")
console.log("Enter array ")
for (let i = 0; i < limit; i++) {
arr.push(jin(""))

}
console.log(arr);
for (let j = 0; j < limit-1; j++) {
arr[j]*=arr[j+1]
}
arr.pop()
console.log(arr);

22. Write a program to add the values of two 2D arrays

a. Program should contains 3 functions including the main function

main()

1. Call function getArray()


2. Call function addArray()
3. Call function displayArray()

getArray()

1. Get values to the array

getArray()

1. Add array 1 and array 2

displayArray()

1. Display the array values


Eg:

Enter the size of array

Enter the values of array 1

1 2

3 4

Enter the values of array 2

5 6

7 8

Output:

Sum of array 1 and array 2:

6 8

10 12

import { question as jin } from 'readline-sync'

function getArray() {

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;
}

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

a. Program should contains 3 functions including the main function

main()

1. Declare an array
2. Call function getArray()
3. Call function displayArray()

getArray()

1. Get values to the array


displayArray()

1. Display the array values

Eg:

Enter the size of array

Enter the array values

1 2 3

4 5 6

7 8 9

Array elements are:

1 2 3

4 5 6

7 8 9

import { question as jin } from 'readline-sync'

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);
}
}

let obj=new Addarray()


let limit = jin("Enter limit ")
console.log("Enter elements of array");
let array=obj.getArray(limit)
obj.displayArray(array)
24. Write a menu driven program to calculate the area of a given object.

a. Program should contain two classes


i. Class 1: MyClass
ii. Class 2: Area
b. Class MyClass should inherit class Area and should contain the following
functions
i. main()
ii. circle()
iii. square()
iv. rectangle()
v. triangle()
c. Class Area should contain the following functions to calculate the area of
different objects
i. circle()
ii. square()
iii. rectangle()
iv. triangle()

Class MyClass extends Area{


public static void main(string args[]){

circle() {

square() {

rectangle() {

triangle() {

Class Area{

circle(){

square(){

rectangle() {

}
triangle() {

Eg 1:

Enter your choice

1. Circle
2. Square
3. Rectangle
4. Triangle

Enter the length

Output

Area of the square is: 4

Eg 2:

Enter your choice

1. Circle
2. Square
3. Rectangle
4. Triangle

Enter the radius

Output

Area of the circle is: 28.26

import {question as jin} from "readline-sync"


class Area {

circle(){

let radius=jin("Enter radius ");


console.log("Area of this circle is :"+2*3.14*radius);
}
rectangle(){

let length=jin("Enter length ");

let breadth=jin("Enter breadth ");


console.log("Area of this rectangle is :"+length*breadth);
}
square(){

let length=jin("Enter length ");


console.log("Area of this square is :"+2*length);
}
triangle(){

let height=jin("Enter height ");

let breadth=jin("Enter breadth ");


console.log("Area of this triangle is :"+1/2*breadth*height);
}
}
class MyClass extends Area{
circle(){
super.circle();
}
square(){
super.square();
}
rectangle(){
super.rectangle();
}
triangle(){
super.triangle();
}
}
let mc=new MyClass();
let exit
while(true){

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:

● If the book is unread:


You still need to read '<book_name>' by <author_name>.
● If the book is read:
Already read '<book_name>' by <author_name>.

var library = [

title: 'Bill Gates',

author: 'The Road Ahead',

readingStatus: true

},

{
title: 'Steve Jobs',

author: 'Walter Isaacson',

readingStatus: true

},

title: 'Mockingjay: The Final Book of The Hunger Games',

author: 'Suzanne Collins',

readingStatus: false

];

import { question as jin } from "readline-sync";


var library = [
{
title: 'Bill Gates',
author: 'The Road Ahead',
readingStatus: true
},
{
title: 'Steve Jobs',
author: 'Walter Isaacson',
readingStatus: true
},
{
title: 'Mockingjay: The Final Book of The Hunger Games',
author: 'Suzanne Collins',
readingStatus: false
}
];
console.log("1.Bill Gates");
console.log("2.Steve Jobs");
console.log("3.Mockingjay: The Final Book of The Hunger Games");
let ch = Number(jin("Take a book by no. "))
switch (ch) {
case 1:
if (library[0].readingStatus==false) {
console.log("You still need to read "+library[0].title+ " by " +library[0].author);
}
else{
console.log("Already read "+library[0].title+ " by " +library[0].author);
}

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:

The statement to print in the tryblock is:

Reversed string is : ${my_string}

The statement to print in the catchblock is:

Error : ${err.message}

The statement to print in the finally block is:


Type of my_string is : ${typeof my_string}

Eg:

a) Sample Input 0

"1234"

Sample Output 0

Reversed string is : 4321

Type of my_string is : string

b) Sample Input 1

Number(1234)

Sample Output 1

Error : my_string.split is not a function

Type of my_string is : number

import { question as jin } from "readline-sync";


let string = jin("Enter something: ")
try{
string=string.split("").reverse().join()
console.log(string);
}
catch(err)
{
console.log(err);
}
string=Number(string)
try{
string.split("").reverse().join()
}
catch(err)
{
console.log(err);
console.error("It must be string to perform string functions");
}
console.log("Type of element you entered is "+typeof string);
27. Given a variable named my_height, you must throw errors under the following
conditions:

● notANumberError- When my_heightis NaN


● HugeHeightError – When my_heightis greater than
● TinyHeight Error - When my_heightis less than

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

import { question as jin } from "readline-sync";


class notANumberError extends Error {
constructor() {
super("Not a number");
this.name = 'notANumberError';
}
}
class TinyHeight extends Error {
constructor() {
super("Your height is less than check point");
this.name = 'TinyHeight';
}
}
class HugeHeightError extends Error {
constructor() {
super("Your height is more than check point");
this.name = 'HugeHeightError';
}
}
let height = jin("Enter your height :")

try{

if(typeof height != "number"){

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:

a. The name of the constructor function should be Car.


b. It should take three parameters: name, mileage and max_speed.
c. Store these parameter values in their respective thiskeywords:
this.name, this.mileage and this.max_speed.
class Car{
constructor (name,mileage,max_speed){
this.name=name
this.mileage=mileage
this.max_speed=max_speed
}

const c1=new Car("BMW M5",10,250)


console.log("Name:"+c1.name);
console.log("Mileage:"+c1.mileage);
console.log("Speed:"+c1.max_speed);

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.

The myFilter function should return the sum of the array.

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);
}

You might also like