Interview Practice Q's JS
Interview Practice Q's JS
// 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
let job = {
jobTitle: 'JavaScript Developer',
location: 'USA'
};
// 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);
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);
// 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);
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
function x(){
console.log("Hello");
}
function y(x){
x();
}
// circumference
// Diameter
// OR ///////
// Question 18: Can you write a function in JavaScript to get the current date in
the format “YYYY-MM-DD”?
// 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);
let a = 10;
let b = 19;
let c = a - (-b);
console.log(c);
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?
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
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
console.log(sum);
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);
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