Skip to content

Under construction

Write a program to find element occurrence in an array

Input:

const arr = [1, 1, 2, 3, 1, 4];

Output:

// 👉 {1: 3, 2: 1, 3: 1, 4: 1}
Answer
const arr = [1, 1, 2, 3, 1, 4];
const count = {};
for (const element of arr) {
if (count[element]) {
count[element] += 1;
} else {
count[element] = 1;
}
}
console.log(count); // 👉 {1: 3, 2: 1, 3: 1, 4: 1}

The code iterates through the array and uses an object count to keep track of the occurrences of each element. The final output is {1: 3, 2: 1, 3: 1, 4: 1}, indicating how many times each number appears in the array.


Write a program to remove duplicate items from an array

Input:

const arr = [1, 2, 3, 4, 1, 2];

Output:

// 👉 [1, 2, 3, 4]
Answer
const arr = [1, 2, 3, 4, 1, 2];
const b = [];
const arr = [1, 2, 3, 4, 1, 2];
arr.filter((dup) => {
if (b.indexOf(dup) === -1) {
b.push(dup);
}
});
console.log("removed array value", b); // 👉 [1, 2, 3, 4]

or just do

console.log([...new Set(arr)]) // 👉 [1, 2, 3, 4]

The code iterates through the array and pushes each element into the new array b only if it is not already present in b. This removes duplicate elements and results in [1, 2, 3, 4].


Write a program for the following output using arrow function

Input:

call(2)(4)(6)

Output:

// 👉 48
Answer
const call = (a) => {
return (b) => {
return (c) => {
return a * b * c
}
}
}
//Above can also be written as a one liner
const call = (a) => (b) => (c) => a * b * c;
console.log("output with arrow function", call(2)(4)(6)); // 👉 48

The arrow function syntax achieves the same result as the normal function, where call(2)(4)(6) multiplies the three numbers together, resulting in 48.


Write a program to return resolve if value is less than 7 using Promise

Input:

fun(5)

Output:

// 👉 "number is given 5"
Answer
function fun(a) {
let myPromise = new Promise((myResolve, myReject) => {
if (a < 7) {
myResolve(`number is given ${a}`);
} else {
myReject("Error");
}
});
myPromise.then((result) => {
console.log(result);
}).catch((error) => {
console.log(error);
});
}
fun(5); // 👉 "number is given 5"

OR this way also possible

const fun = (value) => {
return new Promise((resolve, reject) => {
if(value < 5 ){
resolve('Number less than 5')
} else {
reject('Error')
}
})
}
fun(3).then((success) => console.log(success)).catch((second) => console.log(second))

Write a program to multiply two numbers without using the multiply sign in JavaScript

Input:

const a = 5;
const b = 3;

Expected Output:

// 👉 15
Answer
function multiply(a, b) {
let answer = a;
for (let i = 0; i < b - 1; i++) {
answer += a;
}
return answer;
}
console.log(multiply(5, 3)); // 👉 15

Write a program to sort an array in ascending order

Input:

const arr = [3, 2, 5, 4, 1, 0];

Expected Output:

// 👉 [0, 1, 2, 3, 4, 5]
Answer
const arr = [3, 2, 5, 4, 1, 0];
for (let i = 0; i < arr.length; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
console.log("Elements of array sorted in ascending order:", arr); // 👉 [0, 1, 2, 3, 4, 5]

or

const arr = [3, 2, 5, 4, 1, 0];
const result = arr.sort((a,b) => a - b)
console.log(result)