You can tap to unblur the code solution.
However, we strongly recommend that you try it out yourself first in your code editor.
The sole purpose of such questions is to exercise and enhance your logical and reasoning abilities.
All the best.
Last updated on: 19th June 2023
Code :
function higherPrime(n) {
count = 0;
for (i = 1; i <= n; i++) {
if (n % i == 0) {
count++;
}
}
if (count == 2) {
return n;
} else {
return higherPrime(n + 1);
}
}
console.log(higherPrime(7)); // 7
console.log(higherPrime(14)); // 17
Code :
function longestStr(arr) {
let result = '';
for (i = 0; i < arr.length; i++) {
if (arr[i].length > result.length) {
result = arr[i];
}
}
return console.log(result);
}
longestStr(['bit', 'coin']); // coin
longestStr(['jon', 'snow']); // snow
Code :
function powerOf2(n) {
if (n % 2 == 0) {
for (i = 1; i <= n; i++) {
if (n == 2 ** i) {
return true;
}
}
return false;
} else return false;
}
console.log(powerOf2(16)); // true
console.log(powerOf2(10)); // false
Code :
function lengthOfLastWord(s) {
// Remove leading & trailing spaces
let trimmedStr = s.trim();
// Split string into an array
let arr = trimmedStr.split(' ');
// Get the last word
let result = arr[arr.length - 1];
return console.log(result.length);
}
lengthOfLastWord('Hey JavaScript '); // 10
lengthOfLastWord('Ask padhAI'); // 6
Code :
function stackReverse(str) {
let stack = [];
let result = '';
// turn string into stack
for (i = 0; i < str.length; i++) {
stack[i] = str[i];
}
let length = stack.length;
for (i = length - 1; i >= 0; i--) {
// add every i'th element to result
result = result + stack[i];
// pop last element
length = i;
}
return console.log(result);
}
stackReverse('pikachu'); // uhcakip
stackReverse('ash'); // hsa
Code :
function removeDuplicates(arr1, arr2) {
// merging two arrays using destructuring
let merge = [...arr1, ...arr2];
// sorting the merged array in ascending order
merge.sort((a, b) => a - b);
let result = [];
for (i = 0; i < merge.length; i++) {
if (merge[i] !== merge[i + 1]) {
result.push(merge[i]);
}
}
return console.log(result);
}
removeDuplicates([3, 1, 4], [2, 7, 3]); // [ 1, 2, 3, 4, 7 ]
removeDuplicates([9, 22, 2], [22, 6, 9]); // [ 2, 6, 9, 22 ]
Code :
function theLcm(n1, n2) {
for (i = 1; i <= n2; i++) {
let result = n1 * i;
if (result % n2 == 0) {
return result;
}
}
}
console.log(theLcm(3, 4)); // 12
console.log(theLcm(17, 8)); // 136;
Code :
function consonant(str) {
let count = 0;
str = str.toLowerCase();
for (i = 0; i < str.length; i++) {
if (str[i] == 'a') {
count++;
}
if (str[i] == 'e') {
count++;
}
if (str[i] == 'i') {
count++;
}
if (str[i] == 'o') {
count++;
}
if (str[i] == 'u') {
count++;
}
}
let result = str.length - count;
return console.log(result);
}
consonant('padhakoo'); // 4
consonant('padhAI'); // 3
Code :
function twoSum(arr, n) {
let result = [];
for (i = 0; i < arr.length; i++) {
for (j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] == n) {
result.push(i, j);
}
}
}
return console.log(result);
};
twoSum([2, 7, 11, 15], 9); // [ 0, 1 ]
twoSum([25, 19, 8, 31], 27); // [1, 2 ]
Code :
function insertString(str1, str2) {
// converting a string into array using destructuring
let arr = [...str1];
arr.reverse();
let newArr = [];
newArr.push(arr[0]);
for (i = 1; i < arr.length; i++) {
if (i % 3 != 0) {
newArr.push(arr[i]);
}
else {
newArr.push(str2);
newArr.push(arr[i]);
}
}
return newArr.reverse().join('');
}
console.log(insertString('1234567', '.')); // 1.234.567
console.log(insertString('avengers', '*')); // av*eng*ers
Code :
function romanToInteger(n) {
const romanSymbols = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000,
IV: 4,
IX: 9,
XL: 40,
XC: 90,
CD: 400,
CM: 900,
};
let result = 0;
n.replace(/IV|IX|XL|XC|CD|CM|I|V|X|L|C|D|M/g,
(match) => { result += romanSymbols[match];
}
);
return console.log(result);
}
romanToInteger('III'); // 3
romanToInteger('IX'); // 9
romanToInteger('LVIII'); // 58
romanToInteger('MCMXCIV'); // 1994
Code :
function longestCommonPrefix(arr) {
// Return nothing if arr is empty
if (arr.length == 0) {
return '';
}
for (let i = 0; i <= arr[0].length; i++) {
for (let j = 1; j < arr.length; j++) {
// Check if this character is also present in the same position of each string
if (arr[0][i] !== arr[j][i]) {
// If not, return the string up to and including the previous character
return arr[0].slice(0, i);
}
}
}
return arr[0];
}
console.log(longestCommonPrefix(['padhakoo', 'padhAI', 'paradox'])); // pa
console.log(longestCommonPrefix(['java', 'javascript', 'javelin'])); // jav
Code :
function maxProduct(arr) {
let result = 0;
for (let i = 0; i < arr.length - 1; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (!hasCommonLetters(arr[i], arr[j])) {
const product = arr[i].length * arr[j].length;
result = Math.max(result, product);
}
}
}
return console.log(result);
}
function hasCommonLetters(word1, word2) {
const set1 = new Set(word1);
for (let char of word2) {
if (set1.has(char)) {
return true;
}
}
return false;
}
maxProduct(['abcw', 'foo', 'bar', 'xytf', 'abcdef']); // 16
maxProduct(['ab', 'bc', 'aca', 'abaa']); // 0
Code :
function reverseVowels(s) {
const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
const chars = s.split('');
let start = 0;
let end = chars.length - 1;
while (start < end) {
if (vowels.includes(chars[start]) && vowels.includes(chars[end])) {
[chars[start], chars[end]] = [chars[end], chars[start]];
start++;
end--;
}
else if (vowels.includes(chars[start])) {
end--;
}
else {
start++;
}
}
return chars.join('');
}
console.log(reverseVowels('padhAI')); // pIdhAa
console.log(reverseVowels('padhakoo')); // podhokaa
console.log(reverseVowels('OpenAI')); // IpAneO
Code :
function isPalindrome(s) {
// Convert the string to lowercase and remove non-alphanumeric characters
const str = s.toLowerCase().replace(/[^a-z0-9]/g, '');
// Check if the string is a palindrome
for (let i = 0; i < str.length / 2; i++) {
if (str[i] !== str[str.length - 1 - i]) {
return false;
}
}
return true;
}
console.log(isPalindrome('A man, a plan, a canal: Panama')); // true
console.log(isPalindrome('Was it a car or a cat I saw')); // true
console.log(isPalindrome('padhAI works on GPT 3.5')); // false
Code :
function validParentheses(s) {
const stack = [];
for (let i = 0; i < s.length; i++) {
const char = s[i];
if (char === '(' || char === '[' || char === '{') {
stack.push(char);
} else if (char === ')' || char === ']' || char === '}') {
if (stack.length === 0) {
return false;
}
const top = stack.pop();
if (
(char === ')' && top !== '(') ||
(char === ']' && top !== '[') ||
(char === '}' && top !== '{')
) {
return false;
}
}
}
return stack.length === 0;
}
console.log(validParentheses('()')); // true
console.log(validParentheses('([])')); // true
console.log(validParentheses('{[()]}')); // true
console.log(validParentheses('([)]')); // false
console.log(validParentheses('{[()]}(')); // false
console.log(validParentheses('(([{[]}]))')); // true
console.log(validParentheses('')); // true
console.log(validParentheses('[(])')); // false
Made with
in INDIA