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

Interview Practice Q's JS

Uploaded by

rashireddy981212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Interview Practice Q's JS

Uploaded by

rashireddy981212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

// Question 1: Can you write a function in JavaScript to reverse the order of

words in a given string?

// With Function

function reverseString(str) {
var newString = "";
for (var i = str.length-1; i >= 0; i--) {
newString += str[i];
}
return newString;
}
var d = reverseString('hello');
console.log(d);

// Without Function

var str = "Hello";


var newStr="";
for (var i = str.length-1; i >= 0; i--) {
newStr += str[i];
}
console.log(newStr);

// Question 2: Can you write a function in JavaScript to remove duplicate


elements from an array?

var arr = [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6];


let uniqueArr1 = [];
for (let i = 0; i < arr.length; i++) {
if (uniqueArr1.indexOf(arr[i]) == -1) {
uniqueArr1.push(arr[i]);
}
}
console.log(uniqueArr1);

// Question 3: Write a code to find the largest number in an array

let array = [23, 56, 7, 34, 78, 45, 85];


let max = array[0];

for (let i = 0; i < array.length; i++) {


if (array[i] > max) {
max = array[i];
}
}
console.log(max);

// Question 4: Write a code to find second largest number in an array

let array1 = [1,3,2];


let max1 = array1[0];
let second_max = 0;
for (let i = 0; i < array1.length; i++) {
if (array1[i] > max1 ) {
second_max = max1;
max1 = array1[i];
} else if (array1[i] > second_max && array1[i] != max1) {
second_max = array1[i];
}
}

// Question 5: Write a code to find third largest number in an array

let array2 = [7,6,1,2,3,4];


let max2 = array2[0];
let second_max2 = 0;
let third_max2 = 0;

for (let i = 0; i < array2.length; i++) {


if (array2[i] > max2 ) {
third_max2 = second_max2;
second_max2 = max2;
max2 = array2[i];
} else if (array2[i] > second_max2 && array2[i] != max2) {
third_max2 = second_max2;
second_max2 = array2[i];
}
else if (array2[i] > third_max2 && array2[i] != second_max2) {
third_max2 = array2[i];
}
}
console.log("First max = " +max2);
console.log("Second max = " +second_max2);
console.log("Third max = " +third_max2);

// Question 6: Can you write a function in JavaScript to merge two objects


without overwriting existing properties?
let person = {
firstName: 'John',
lastName: 'Doe',
age: 25,
jobTitle: 'JavaScript Developer',
ssn: '123-456-2356'
};

let job = {
jobTitle: 'JavaScript Developer',
location: 'USA'
};

let employee = {...person, ...job};


console.log(employee);

// Question 7: Compound Interest Calculation

let principal = 800000;


rate = 6;
time = 3;
let A = principal * (Math.pow((1 + rate / 100), time));
let CI = parseFloat(A - principal).toFixed(2);

console.log("Compound Interest = " + CI);

// Question 8: Program to store name, phone number and marks of a student using
objects

const student1 = {
name: "jenny",
phone_no: "2343243234",
marks: "87"
}
console.log(student1);

// Question 9: Create a variable of type string and try to add a number to it

var a = "Hello";
var b = 10;
var c = a+b;
console.log(c);
// Question 10: Using typeof operator to find a datatype in javascript using
program 2.
console.log(typeof a);
console.log(typeof b);
console.log(typeof c);

// Question 11: Create a const object in javascript can you change it to hold a
number later

// const student = {
// name: "jenny"
// }
// const student = {
// number: 10
// }
// console.log(student);

// Here we cannot change the const because it can neither be updated or


redeclared. It must be initialized during the declaration itself unlike var and
let.

// Question 12: Try a to add a newkey to the const object in previous program.
Will it work now??

const student = {
name: "jenny"
}
const new_student = {
number: 10
}
console.log(student);
console.log(new_student);

// Question 13: Write a JS program to create a word meaning dictionary of 5 words

let mydict = {
mydict_Answer: "The response or receipt to a phone call, question, or
letter",
mydict_Amount: "A mass or a collection of something",
mydict_Ban: "An act prohibited by social pressure or law.",
mydict_Care: "Extra responsibility and attention",
mydict_Chip: "A small and thin piece of a larger item"
}
console.log(mydict);
console.log(mydict.mydict_Answer);
console.log(mydict.mydict_Amount);
console.log(mydict.mydict_Ban);
console.log(mydict.mydict_Care);
console.log(mydict.mydict_Chip);

// Question 14: Use logical operators to find whether the age of a person lies
between 10 and 20

var age = parseInt(prompt("Enter your age"));


if (age>=10 && age<=20) {
console.log("Your age lies between 10 and 20");
}
else {
console.log("Your age doesn't lie between 10 and 20");
}

// Question 15: Demonstrate the use of switch case statements in JS

// The switch statement is used to perform different actions based on different


conditions

var age = parseInt(prompt("Enter your age"));


switch(age){
case 11:
console.log("Your age is 11")
break;
case 12:
console.log("Your age is 12")
break;
case 13:
console.log("Your age is 13")
break;
case 14:
console.log("Your age is 13")
break;
case 15:
console.log("Your age is 15")
break;
default:
console.log("Your age is special");
}

// Question 16: Higher order function

function x(){
console.log("Hello");
}

function y(x){
x();
}

// Question 17: Program to calculate area of 4 circles, circumference, diameter

const radius = [5, 4, 3, 6];


const area = function (radius) {
const output = [];
for (let i = 0; i < radius.length; i++) {
output.push(Math.PI * radius[i] * radius[i]);
}
return output;
}
console.log(area(radius));

// circumference

const circumference = function (radius) {


const output = [];
for (let i = 0; i < radius.length; i++) {
output.push(2 * Math.PI * radius[i]);
}
return output;
}
console.log(circumference(radius));

// Diameter

const Diameter = function (radius) {


const output = [];
for (let i = 0; i < radius.length; i++) {
output.push(2 * radius[i]);
}
return output;
}
console.log(Diameter(radius));

// OR ///////

const radius1 = [5, 4, 3, 6];

const area1 = function (radius1) {


return Math.PI * radius1 * radius1;
};

const circumference1 = function (radius1) {


return 2 * Math.PI * radius1;
};

const diameter = function (radius1) {


return 2 * radius1;
};

const calculate = function (radius1, length) {


const output = [];
for (let i = 0; i < radius1.length; i++) {
output.push(logic(radius1[i]));
}
return output;
};
console.log(radius1.map(area1));
console.log(radius1.map(circumference1));
console.log(radius1.map(diameter));

// Question 18: Can you write a function in JavaScript to get the current date in
the format “YYYY-MM-DD”?

let date1 =new Date();


console.log(date1);
let dd = date1.getDate();
let mm = date1.getMonth() + 1;
let yyyy = date1.getFullYear();
let current_date = yyyy + '/' + mm + '/' + dd;
console.log(current_date);

// Question 19: Write a code find the occurance of each unique character
let name2 = "ChaithanyaSRaasiP"
let uniqueChar2 = {};
for (let i=0; i<name2.length; i++) {
let word = name2.charAt(i)
if(!uniqueChar2[word]) {
uniqueChar2[word] = 1
}
else {
uniqueChar2[word]+=1
}
}
console.log(uniqueChar2);

// Question 20: Add 2 numbers without using + symbol

let a = 10;
let b = 19;
let c = a - (-b);
console.log(c);

// Question 21: Can you write a function in JavaScript to calculate the


cumulative sum of an array?

function cumulate() {
let cArray = [3,5,7,8,9];
let cumsum = [cArray[0]];
for (i=1; i<cArray.length; i++) {
cumsum.push(cArray[i] + cumsum [i-1]);
}
return cumsum;
}
var d = cumulate();
console.log(d);

// Question 22: Can you write a function in JavaScript to split an array into
chunks of a specified size?

let arraychunk = [1,3,5,6,7,3,6,8,9];


let res = [];
let chunksize = 3;
for ( let i=0; i<arraychunk.length; i+=chunksize) {
const chunks = arraychunk.slice(i, i + chunksize)
res.push(chunks)
}
console.log(res);
// Question 23: Program to find prime numbers

var prime = parseInt(prompt("Enter a Number"));


let isPrime1 = true;
for(let i=2; i<prime; i++) {
if (prime % 2 == 0) {
isPrime1 = false;
break;
}
}
if (isPrime1 == true ) {
console.log(prime + " " + "is a Prime Number");
} else {
console.log(prime + " " + "is not a Prime Number");
}

// Question 24: program to generate fibonacci series up to n terms

const number = parseInt(prompt('Enter the number of terms: '));


let n1 = 0;
let n2 = 1;
let nextTerm;
console.log('Fibonacci Series:');
for (let i = 1; i <= number; i++) {
console.log(n1);
nextTerm = n1 + n2;
n1 = n2;
n2 = nextTerm;

// Question 25: program to generate fibonacci series starting with 10 and 11

const number1 = parseInt(prompt('Enter the number of terms: '));


let n01 = 10;
let n02 = 11;
let nextTerm1;
console.log('Fibonacci Series:');
for (let i = 1; i <= number1; i++) {
console.log(n01);
nextTerm1 = n01 + n02;
n01 = n02;
n02 = nextTerm1;
}

// Question 26: Program to find prime numbers

var prime = parseInt(prompt("Enter a Number"));


let isPrime = true;
for(let i=2; i<prime; i++) {
if (prime % 2 == 0) {
isPrime = false;
break;
}
}
if (isPrime == true ) {
console.log(prime + " " + "is a Prime Number");
} else {
console.log(prime + " " + "is not a Prime Number");
}

// Question 27: Program to find top 1000 prime numbers

const prime = [];


let currentNumber = 2;
while (prime.length < 1000) {
let isPrime = false;
for (let i=2; i < currentNumber; i++) {
if (currentNumber !== i && currentNumber % i === 0) {
currentNumber++;
isPrime = true;
break;
}
}
if (isPrime) {
continue;
}
prime.push(currentNumber);
currentNumber++;
}
console.log(prime);

// Question 28: Program to find Even number and odd number

let userInput = parseInt(prompt("Enter a number:"));


if (userInput % 2 == 0) {
console.log(userInput +" "+ "is an Even Number");
} else {
console.log(userInput +" "+ "is an Odd Number");
}

// Question 29: Program to find occurance of each number from an array of


duplicate numbers

const myArray = [1, 2, 3, 1, 2, 4, 5, 1, 2, 2];


function countDuplicates(arr) {
const counts = {};
arr.forEach((value) => {
if (!counts[value]) {
counts[value] = 1;
} else {
counts[value]++;
}
});
return counts;
};
const result = countDuplicates(myArray);
console.log(result);

// Question 30: Program to reverse each word from a sentence

function reverseWordsShorter(input)
{
var word = "", output = "";
for(var i = input.length - 1; i >= 0; i--) {
if (input[i] == " ") {
output = " " + word + output;
word = "";
}
else {
word += input[i];
}
}
return word + output;
}
var d = reverseWordsShorter('hello world, Goodmorning');
console.log(d);

// Question 31: Program to find number of vowels in a string and it's occurance

function getVowelCount (inputString) {


const string = inputString.toLowerCase();
const vowelCount = { a: 0, e: 0, i: 0, o: 0, u: 0 };
for (let i = 0; i < string.length; i++) {
const character = string[i];
switch (character) {
case "a":
vowelCount.a += 1;
break;
case "e":
vowelCount.e += 1;
break;
case "i":
vowelCount.i += 1;
break;
case "o":
vowelCount.o += 1;
break;
case "u":
vowelCount.u += 1;
break;
}
}
const { a, e, i, o, u } = vowelCount;
const result = `Vowel Count:: a:${a}, e:${e}, i:${i}, o:${o}, u:${u}.`;
return result;
};
console.log(getVowelCount("Oppenheimer"));

// Question 32: Program to find highest number from a jagged array

let array5 = [2, 4, 10, [12, 4, [105, 99], 4], [3, 2, 99], 0 ]
let max5 = 0
function maxNumber(array5) {
for (let i = 0; i < array5.length; i++) {
if (Array.isArray(array5[i])) {
maxNumber(array5[i])
} else {
if (array5[i] > max5) {
max5 = array5[i]
}
}
}
}
maxNumber(array5)
console.log(max5)
// Question 33: Program to find duplicate numbers in a n array both ascending and
descending order

const array6 = [2, 4, 1, 2, 3, 2, 3, 4, 5, 5, 6, 4];


let uniqueArr = [];
for(let i of array6) {
if(uniqueArr.indexOf(i) === -1) {
uniqueArr.push(i);
}
}
console.log("Array without duplicate values =" +" "+ uniqueArr);
let asc = uniqueArr.sort((a, b) => a - b)
console.log("Array sorted in Ascending order =" +" "+ asc);
let dec = uniqueArr.sort((a, b) => b - a)
console.log("Array sorted in Decending order =" +" "+ dec);

// Question 34: Program to find sum of all the elements in an array

// Using Normal Logic


const arr = [23, 34, 77, 99, 324];
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
}

console.log(sum);

// Using inbuilt function


const arra = [23, 34, 77, 99, 324, 22, 45];
let sumof = 0;
arra.forEach((el) => sumof += el);
console.log(sumof);

// Question 35: Program to find prime numbers using recursive function

function primeNumberRecursive (n, i = 2) {


if(n <= 2){
return (n == 2 )? true : false;
}
if( n % i == 0){
return false;
}
if( i * i > n ){
return true;
}
return primeNumberRecursive(n, i+1);
}
let v = primeNumberRecursive(2)
console.log(v);

// Question 36: Write a JS program to find whether a number is divisible by 2 and


3

function checkDivisibility(number) {
if (number % 2 === 0 && number % 3 === 0) {
console.log(number + " is divisible by 2 and 3");
} else {
console.log(number + " is not divisible by 2 and 3")
}
}
let n = checkDivisibility(18);
console.log(n);

// Question 37: Write a JS program to find whether a number is divisible by


either 2 and 3

function checkDivisibility(number) {
if (number % 2 === 0 || number % 3 === 0) {
console.log(number + " is divisible by 2 or 3");
} else {
console.log(number + " is not divisible by neither 2 nor 3")
}
}
let z = checkDivisibility(5);
console.log(z);

// Question 38: Print "You can Drive" or "You cannot Drive" based on age being
greater than 18 using ternary operator

let age = parseInt(prompt("Enter your age"));


let result1 = (age > 18) ?
"You can Drive" : "You cannot Drive";
console.log(result1);

You might also like