Javascript - Domain Fundamentals Assignments

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 43

Assignments

1. Accept a char input from the user and display it on the console.

Code of the program & screenshot of the output.

2. Accept two inputs from the user and output their sum.

Variable Data Type

Number 1 Integer
Number 2 Float

Sum Float

Code of the program & screenshot of the output.

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

Code of the program & screenshot of the output.

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

Code of the program & screenshot of the output.

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

Code of the program & screenshot of the output.


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

Code of the program & screenshot of the output.


const prompt=require("prompt-sync")({signint:true});
console.log(" 1. Sunday 2. Monday 3. Tuesday 4. Wednesday 5.
Thursday 6. Friday 7. Saturday");
var a=parseInt(prompt("Entert your choice"));
switch(a)

case 1:console.log("You have selected Sunday");

break;

case 2:console.log("You have selected Monday");

break;

case 3:console.log("You have selected Tuesday");

break;

case 4:printf("You have selected Wednesday");


break;

case 5:console.log("You have selected Thursday");

break;

case 6:console.log("You have selected Friday");

break;

case 7:console.log("You have selected Saturday");

break;

default:console.log("Look carefully");

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

Code of the program & screenshot of the output.


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

Code of the program & screenshot of the output.


9. Write a program to print the following pattern (hint: use nested loop)

12

123

1234

12345

Code of the program & screenshot of the output.


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

Code of the program & screenshot of the output.


const prompt=require("prompt-sync")({signint:true});
var a=parseInt(prompt("Enter a limit"));
var b=[];
var c=[];
var d=[];
var temp;
prompt("Enter the first array");
for(var i=0;i<a;i++){
b[i]=prompt();
}
prompt("Enter the second array");
for(var i=0;i<a;i++){
c[i]=prompt();
}
for(var i=0;i<a;i++){
d[i]=b[i];
b[i]=c[i];
c[i]=d[i];
}
console.log(a);
console.log(b);
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

Code of the program & screenshot of the output.


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

Code of the program & screenshot of the output.


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

Code of the program & screenshot of the output.


14. Write a program to add to 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

Code of the program & screenshot of the output.


var input = require("readline-sync")
const prompt=require("prompt-sync")({signint:true});
var size = Number(input.question("Enter the 2d aray"))
let a = new Array(size)
let b = new Array(size)
let c = new Array(size)
for (var i = 0; i < size; i++) {
a[i] = new Array(size)
b[i] = new Array(size)
c[i] = new Array(size)
}
console.log("Enter the values of array1:")
for(var i=0;i<size;i++){
for(j=0;j<size;j++){
a[i][j]=Number(input.question())
}
}
console.log("Enter the values of array2:")
for(var i=0;i<size;i++){
for(j=0;j<size;j++){
b[i][j]=Number(input.question())
}
}
for(var i=0;i<size;i++){
for(j=0;j<size;j++){
c[i][j]=a[i][j] + b[i][j]
}
}
console.log("Sum of Arrays:");
for(var i=0;i<size;i++){
for(j=0;j<size;j++){
process.stdout.write(c[i][j]+" ")
}
console.log()
}

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

Code of the program & screenshot of the output.


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

Code of the program & screenshot of the output.

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

Code of the program & screenshot of the output.


let input = require("readline-sync")
const prompt=require("prompt-sync")({signint:true});
class Maths{
addition(a,b){
return a + b;
}
subtraction(a,b){
return a - b;
}
multiplication(a,b){
return a * b;
}
division(a,b){
return a / b;
}
}
let d = new Maths()
main();
function main() {
let a = Number(input.question("Enter the First Number:"))
let b = Number(input.question("Enter the Second Number:"))
let choice = Number(input.question("1.Addition\n2.Subtraction\
n3.Multiplication\n4.Division\nEnter Your Choice:"))

switch (choice) {
case 1:
var result = d.addition(a,b)
console.log("Result:"+result);
break;
case 2:
var result = d.subtraction(a,b)
console.log("Result:"+result);
break;
case 3:
var result = d.multiplication(a,b)
console.log("Result:"+result);
break;
case 4:
var result = d.division(a,b)
console.log("Result:"+result);
break;

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

Code of the program & screenshot of the output.


let input = require("readline-sync")
const prompt=require("prompt-sync")({signint:true});
var a = parseInt(prompt("Written test mark"));
var b = parseInt(prompt("Lab Mark"));
var c= parseInt(prompt("Assignment mark"));

var d = parseFloat(a*70)/100 + (b*20)/100 + (c*10)/100


console.log("Grade of the student is:"+d);

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

Code of the program & screenshot of the output


let input = require("readline-sync")
const prompt=require("prompt-sync")({signint:true});
let a = parseInt(prompt("Enter your annual income "));

if(a<=250000 && a>=0){


console.log("No Tax");
}else if(a<=500000 && a>250000){
var b = (a*5)/100
console.log("Income tax payable:"+b);
}else if(a<=1000000 && a>500000){
var bt = (a*20)/100
console.log("Income tax payable:"+b);
}else if(a<=5000000 && a>1000000){
var b = (a*30)/100
console.log("Income tax payable:"+b);
}else{
console.log("Kodeeshwaraaa");
}

20. Write a program to print the following pattern using for loop

2 3

4 5 6

7 8 9 10

Code of the program & screenshot of the output.


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

Code of the program & screenshot of the output.


let input = require("readline-sync")
const prompt=require("prompt-sync")({signint:true});
let size = Number(input.question("Enter array limit:"))
let array = new Array(size)
let sum = new Array(size-1)
console.log("Enter elements:")
for(let i = 0 ;i < size; i++){
array[i] = Number(input.question())
}
for (let j = 0; j < size-1; j++) {
sum[j] = array[j] * array[j+1]
}

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

Code of the program & screenshot of the output

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

Code of the program & screenshot of the output

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

Code of the program & screenshot of the output

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

];

Code of the program & screenshot of the output.

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

Code of the program & screenshot of the output.


27. Given a variable named userHeight, you must throw errors under the following
conditions:
● notANumberError- When userHeight is NaN
● HugeHeightError – When userHeight is greater than 200
● TinyHeightError - When userHeight is less than 40

Eg:

a) Sample Input 0

test

Sample Output 0

notANumberError

b) Sample Input 1

250

Sample Output 1

hugeHeightError

c) Sample Input 2

Sample Output 2

tinyHeightError

d) If userHeight is valid print ‘valid’

Code of the program & screenshot of the output.


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.

Code of the program & screenshot of the output.

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

Code of the program & screenshot of the output.

You might also like