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

Coding Problem Solutions 1-3

The document contains 3 coding problems and their solutions in JavaScript. The first problem demonstrates performing basic math operations (addition, subtraction, multiplication, division) on two variables and logging the results. The second problem checks if a given number is even or odd. The third problem uses a switch case to check if a given number exists in a sample array.

Uploaded by

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

Coding Problem Solutions 1-3

The document contains 3 coding problems and their solutions in JavaScript. The first problem demonstrates performing basic math operations (addition, subtraction, multiplication, division) on two variables and logging the results. The second problem checks if a given number is even or odd. The third problem uses a switch case to check if a given number exists in a sample array.

Uploaded by

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

Coding Problem 1

Write a JavaScript program to do the following:


1. Create two variables and assign the values 35 and 5.
2. Perform the following operations:
a. Addition
b. Subtraction
c. Multiplication
d. Division
3. Assign the results of these operations into new variables.
4. Display these results in the browser console.

Solution Code:

const number1 = 35;


const number2 = 5;

const add_ans = number1 + number2;


console.log(`${number1} + ${number2} = ${add_ans}`);

const sub_ans = number1 - number2;


console.log(`${number1} - ${number2} = ${sub_ans}`);

const mul_ans = number1 * number2;


console.log(`${number1} * ${number2} = ${mul_ans}`);

const dvd_ans = number1 + number2;


console.log(`${number1} / ${number2} = ${dvd_ans}`);

Full Stack Development Specialization training Page no 1 of 3


Coding problem 2

Q. Write a program to check the given number (const num = 2541;) is odd or even

Solution Code:

const num = 2541;

if (num % 2 == 0) {
console.log("The given number is even.");
} else {
console.log("The given number is odd.");
}

Full Stack Development Specialization training Page no 2 of 3


Coding Problem 3

Q. Write a program to find whether the given number is present in the given array or not using
the switch case.
Input:
const arr = [2,4,5,9,3,1,8];
const num = 5;

Solution code:
CP23.js
const arr = [2,4,5,9,3,1,8];
const num = 5;
switch (num) {
case (arr[0]):
console.log('num is found');
break;
case (arr[1]):
console.log('num is found');
break;
case (arr[2]):
console.log('num is found');
break;
case (arr[3]):
console.log('num is found');
break;
case (arr[4]):
console.log('num is found');
break;
case (arr[5]):
console.log('num is found');
break;
case (arr[6]):
console.log('num is found');
break;
default:
console.log('num is not found');
}

Full Stack Development Specialization training Page no 3 of 3

You might also like