Skip to content

Commit d12debb

Browse files
committed
new stuff
1 parent 231a394 commit d12debb

23 files changed

+240
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function calculator(operation, num1, num2) {
2+
switch (operation) {
3+
case 'add':
4+
return num1 + num2;
5+
case 'subtract':
6+
return num1 - num2;
7+
case 'multiply':
8+
return num1 * num2;
9+
case 'divide':
10+
if (num2 === 0) {
11+
return 'Error: Division by zero';
12+
}
13+
return num1 / num2;
14+
default:
15+
return 'Error: Invalid operation';
16+
}
17+
}
18+
19+
// Usage
20+
console.log(calculator('add', 5, 3)); // Output: 8
21+
console.log(calculator('subtract', 5, 3)); // Output: 2
22+
console.log(calculator('multiply', 5, 3)); // Output: 15
23+
console.log(calculator('divide', 5, 3)); // Output: 1.6666666666666667
24+
console.log(calculator('divide', 5, 0)); // Output: Error: Division by zero
25+
console.log(calculator('unknown', 5, 3)); // Output: Error: Invalid operation
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const arr1 = [1, 2, 3];
2+
const arr2 = [1, 2, 3];
3+
const arr3 = [1, 2, 4];
4+
5+
function arraysEqual(arr1, arr2) {
6+
if (arr1.length !== arr2.length) {
7+
return false;
8+
}
9+
for (let i = 0; i < arr1.length; i++) {
10+
if (arr1[i] !== arr2[i]) {
11+
return false;
12+
}
13+
}
14+
return true;
15+
}
16+
17+
function arraysEqual(arr1, arr2) {
18+
return JSON.stringify(arr1) === JSON.stringify(arr2);
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// "==" check for value
2+
// "===" check for value and type
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const arr = [1, 4, 5, 3];
2+
const value = (arr) => {
3+
findLarge = Math.max(...arr);
4+
index = arr.indexOf(findLarge);
5+
arr.splice(index, 1);
6+
return arr;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const arr = [1, 2, 3, 4, 1, 2, 5];
2+
const ans = arr.filter((ele, idx, arr) => arr.indexOf(ele) !== idx);
3+
console.log(ans);
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// filter: Creates a new array with all elements that pass the test implemented by the provided function.
2+
3+
const people = [
4+
{ name: "John", age: 24 },
5+
{ name: "Jane", age: 30 },
6+
{ name: "Mike", age: 27 },
7+
{ name: "Sarah", age: 22 },
8+
];
9+
10+
const olderThan25 = people.filter(x => x.age > 25);
11+
12+
console.log(olderThan25);

CodingInterviewQuestions/FindEven.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const arr = [1, 122, 31, 44, 78];
2+
const evenNumbers = arr.filter(x => x%2==0);
3+
4+
console.log(evenNumbers);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const input = prompt("Please enter")
2+
var fact = 1;
3+
for (let i = 1; i <= input; i++){
4+
fact = fact * i;
5+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const people = [
2+
{ name: "John", age: 24 },
3+
{ name: "Jane", age: 30 },
4+
{ name: "Mike", age: 27 },
5+
{ name: "Sarah", age: 22 },
6+
];
7+
8+
const firstOlderThan25 = people.find(x => x.age > 25);
9+
10+
console.log(firstOlderThan25);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const arr = [1, 2, 19, 0, 4, 7];
2+
const miss = [];
3+
4+
const missing = (arr) => {
5+
const missMin = Math.min(...arr);
6+
const missMax = Math.max(...arr);
7+
for (let i = missMin; i < missMax; i++){
8+
if (arr.indexOf(i) < 0) {
9+
miss.push(i);
10+
}
11+
}
12+
return miss;
13+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function countCharacterOccurrences(str) {
2+
const map = new Map();
3+
for (const x of str) {
4+
if (map.has(x)) {
5+
map.set(x, map.get(x) + 1);
6+
} else {
7+
map.set(x, 1);
8+
}
9+
}
10+
return map;
11+
}
12+
const str = "hello world";
13+
const occurrences = countCharacterOccurrences(str);
14+
console.log(occurrences);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const str1 = "olleh";
2+
const str2 = "hello";
3+
const reversedStr = str2.split('').reverse().join('');
4+
5+
if (str1 === reversedStr) console.log('YEs');
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const input = prompt("Please enter")
2+
var fact = 1;
3+
var flag = 0;
4+
for (let i = 2; i < input; i++){
5+
if (input % i == 0) {
6+
flag = 1;
7+
break;
8+
}
9+
}
10+
if (flag === 0) {
11+
console.log('Not Prime');
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const arr = [10, 5, 40, 25];
2+
arr.sort((a, b) => b - a); // Sort the array in descending order
3+
let x = arr[1]; // Get the second largest element
4+
console.log(x); // Output: 25

CodingInterviewQuestions/FindUnion.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const arr1 = [1, 2, 3];
2+
const arr2 = [3, 4, 5];
3+
4+
function arraysUnion(arr1, arr2) {
5+
const combined = [...arr1, ...arr2];
6+
7+
// Create a Set from the combined array to remove duplicates
8+
const uniqueSet = new Set(combined);
9+
10+
// Convert the Set back to an array using Array.from
11+
return Array.from(uniqueSet);
12+
}
13+
const union = arraysUnion(arr1, arr2);
14+
console.log(union);
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var string = prompt("enter");
2+
const vowels = ['a', 'e', 'i', 'o', 'u'];
3+
const countVowel = (str) => {
4+
var c = 0;
5+
for (let i of str.toLowerCase()) {
6+
if (vowels.includes(i)) {
7+
c++;
8+
}
9+
}
10+
return c;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function capitalizeFirstLetter(str) {
2+
if (str.length === 0) return str; // Return the original string if it's empty
3+
4+
return str.charAt(0).toUpperCase() + str.slice(1);
5+
}
6+
7+
// Usage
8+
const result = capitalizeFirstLetter('hello');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const arr1 = [1, 2, 3];
2+
const arr2 = [11, 12, 3];
3+
4+
function arraysIntersects(arr1, arr2) {
5+
let x = Math.max(arr1.length, arr2.lengt);
6+
for (let i = 0; i < x; i++) {
7+
if (arr1[i] === arr2[i]) {
8+
return arr1[i];
9+
}
10+
}
11+
return -1;
12+
}
13+
14+
15+
function arraysIntersection(arr1, arr2) {
16+
// Create a Set from the second array for fast lookup
17+
const set2 = new Set(arr2);
18+
19+
// Filter the first array to include only elements that are also in set2
20+
return arr1.filter(value => set2.has(value));
21+
}

CodingInterviewQuestions/MaxAndMin.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const arr = [1, 122, 31, 44, 78];
2+
const maxFunction = (arr) => {
3+
if (!Array.isArray(arr) || arr.length === 0) {
4+
throw new Error("The input must be a non-empty array.");
5+
}
6+
return arr.reduce((prev, curr) => prev > curr ? prev : curr, -Infinity);
7+
}
8+
9+
console.log(maxFunction(arr));
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const arr1 = [1, 2, 3];
2+
const arr2 = [11, 12, 13];
3+
4+
const res = [...arr1, ...arr2];
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const str1 = "hello";
2+
let reversedStr1 = "";
3+
4+
for (let i = str1.length - 1; i >= 0; i--) {
5+
reversedStr1 += str[i];
6+
}
7+
8+
console.log(reversedStr);
9+
10+
11+
const str2 = "hello";
12+
const reversedStr2 = str2.split('').reverse().join('');
13+
console.log(reversedStr2); // Output: "olleh"
14+
// Explanation:
15+
// str.split(''): Converts the string into an array of characters.
16+
17+
// Example: "hello" becomes ['h', 'e', 'l', 'l', 'o'].
18+
// reverse(): Reverses the order of the elements in the array.
19+
20+
// Example: ['h', 'e', 'l', 'l', 'o'] becomes ['o', 'l', 'l', 'e', 'h'].
21+
// join(''): Joins the elements of the array back into a single string.
22+
23+
// Example: ['o', 'l', 'l', 'e', 'h'] becomes "olleh".
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const arr = [1, 122, 31, 44, 78];
2+
const sum = arr.reduce(
3+
(accumulator, currentValue) => accumulator + currentValue,
4+
0
5+
);
6+
7+
console.log(sum);

CodingInterviewQuestions/Swap.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let a = 5;
2+
let b = 10;
3+
4+
[a, b] = [b, a];
5+
6+
console.log(a); // Output: 10
7+
console.log(b)

0 commit comments

Comments
 (0)