JavaScript | Interview Preparation
Problem- 1:
Create a function named getLetter(s)
. It has one parameter: a string, , consisting of lowercase English alphabetic letters (i.e., a
through z
). It must return A
, B
, C
, or D
depending on the following criteria:
- If the first character in string is in the set {a,e,i,o,u} , then return
A
. - If the first character in string is in the set {b,c,d,f,g} , then return
B
. - If the first character in string is in the set {h,j,k,l,m} , then return
C
. - If the first character in string is in the set {n,p,q,r,s,t,v,w,x,y,z} , then return
D
.
Solution:
function getLetter(s) {
let letter;
switch(s[0]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
letter ='A';
break; case 'b':
case 'c':
case 'd':
case 'f':
case 'g':
letter = 'B';
break; case 'h':
case 'j':
case 'k':
case 'l':
case 'm':
letter = 'C';
break;
case 'n':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
letter = 'D';
break; default:
'None'
}
return letter;
}
Problem- 2:
Implement a function named factorial that has one parameter: an integer, . It must return the value of (i.e., factorial).
Solution:
function factorial(n) { if(n < 1) { return 1 } return n*factorial(n-1); //we use recursive here}
Problem- 3:
Create a function named vowelsAndConsonants
. It has one parameter, a string, , consisting of lowercase English alphabetic letters (i.e., a
through z
). The function must do the following:
i) First, print each vowel in on a new line. The English vowels are a, e, i, o, and u, and each vowel must be printed in the same order as it appeared in .
ii) Second, print each consonant (i.e., non-vowel) in on a new line in the same order as it appeared in .
Solution:
function vowelsAndConsonants(s) {
const arr = s.split('');
const vowels = ['a','e','i','o','u'];
let consonant = []; for (let i = 0, len = arr.length; i < len ; i++) {
let letter = arr[i];
if(vowels.indexOf(letter) !== -1) {
console.log(letter);
} else {
consonant.push(letter)
}
} for(let x = 0, len = consonant.length; x < len; x++) {
console.log(consonant[x]);
}
}
Problem- 4:
Write a function getSecondLargest
. It has one parameter: an array, nums
, of n numbers. The function must find and return the second largest number in .
Solution:
function getSecondLargest(nums) {
const firstMax = Math.max(...nums)
const arr = nums.filter(num => num !== firstMax);
const secondMax = Math.max(...arr); return secondMax;
}
Problem- 5:
Create a function named reverseString
; it has one parameter, s. You must perform the following actions:
- Try to reverse string s using the split, reverse, and join methods.
- If an exception is thrown, catch it and print the contents of the exception’s message on a new line.
- Print s on a new line. If no exception was thrown, then this should be the reversed string; if an exception was thrown, this should be the original string.
Solution:
function reverseString(s) {
try {
s = s.split('').reverse().join('');
} catch(error) {
console.log(error.message);
} finally {
console.log(s);
}
}
Problem- 6:
Create a function isPositive
. It has one integer parameter, a. If the value of a is positive, it must return the string YES
. Otherwise, it must throw an Error according to the following rules:
- If a is 0, throw an Error with message =
Zero Error
. - If a is negative, throw an Error with message =
Negative Error
.
Solution:
function isPositive(a) {
if(a > 0) {
return "YES"
} else if(a === 0) {
throw new Error("Zero Error");
} else {
throw new Error("Negative Error")
}
}
Problem- 7:
Create a Polygon class that has the following properties:
- A constructor that takes an array of integer values describing the lengths of the polygon’s sides.
- A perimeter() method that returns the polygon’s perimeter.
Solution:
class Polygon {
constructor(arr) {
this.sides = arr;
}
perimeter(){
let sum = 0;
this.sides.map(side => sum += side);
return sum;
}
}
Problem- 8:
We provide the implementation for a Rectangle
class .
class Rectangle {
constructor(w, h) {
this.w = w;
this.h = h;
}
}
Perform the following tasks:
- Add an area method to Rectangle’s prototype.
- Create a Square class that satisfies the following:
- It is a subclass of Rectangle.
- It contains a constructor and no other methods.
- It can use the Rectangle class’ area method to print the area of a Square object.
Solution:
class Rectangle {
constructor(w, h) {
this.w = w;
this.h = h;
}
}Rectangle.prototype.area = function() {
return this.w * this.h;
}class Square extends Rectangle{
constructor(side) {
super(side);
this.w = side;
this.h = side;
}
}
Problem- 9:
We have a tagged template literal that passes the area and perimeter of a rectangle to a tag function named sides. Recall that the first argument of a tag function is an array of string literals from the template, and the subsequent values are the template’s respective expression values.
function sides(literals, ...expressions) {}
Complete the function in the editor so that it does the following:
- Finds the initial values of and by plugging the area and perimeter values into the formula:where is the rectangle’s area and is its perimeter.
- Creates an array consisting of and and sorts it in ascending order.
- Returns the sorted array.
Solution:
function sides(literals, ...expressions) {
const [A, P] = expressions;
const x = Math.sqrt(P*P -16 * A)
const s1 = (P - x) / 4;
const s2 = (P + x) /4; return [s1, s2];
}
Problem- 10
Create a function by returning a RegExp object, re , that matches any string that begins and ends with the same vowel. Recall that the English vowels are a
, e
, i
, o
, and u
.
Constraints
- The length of string s is ≥ 3 .
- String s consists of lowercase letters only (i.e.,
[a-z]
).
Solution:
function regexVar() {
let re = /^([aeiou]).+\1$/;
return re;
}//^ for matching 0th index
//() stores matching value captured
//[aeiou] any of this
//.+ more
//\1 match the previously matching
//$ match the last
That’s all for today..
Thank you for reading🥰