Skip to content

Commit 826e2e0

Browse files
committed
new stuff
1 parent d12debb commit 826e2e0

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const valueToFind = parseInt(input, 10);
2+
const array = [1, 2, 3, 4, 2, 5, 2];
3+
4+
const firstIndex = array.indexOf(valueToFind);
5+
if (firstIndex !== -1) {
6+
console.log(`First occurrence of ${valueToFind} is at index ${firstIndex}`);
7+
} else {
8+
console.log(`${valueToFind} not found in the array`);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const obj = {
2+
a: 'apple',
3+
b: 'banana',
4+
c: 'apple',
5+
d: 'orange',
6+
e: 'banana',
7+
};
8+
9+
const m = new Map();
10+
11+
for (const x in obj) {
12+
const v = obj[x];
13+
if (m.has(v)) {
14+
m.set(v, m.get(v)+1);
15+
} else {
16+
m.set(v, 1);
17+
}
18+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Creating a polyfill for Array.prototype.forEach in JavaScript is a great exercise to understand how this method works. The forEach method iterates over each element in an array and executes a provided function once for each element.
2+
3+
// Here's a basic polyfill for Array.prototype.forEach:
4+
5+
6+
if (!Array.prototype.forEach) {
7+
Array.prototype.forEach = function(callback, thisArg) {
8+
// Check if callback is a function
9+
if (typeof callback !== 'function') {
10+
throw new TypeError(callback + ' is not a function');
11+
}
12+
13+
// Get the array that forEach was called on
14+
var array = Object(this);
15+
var length = array.length >>> 0; // Convert to unsigned 32-bit integer
16+
17+
// Iterate over the array
18+
for (var i = 0; i < length; i++) {
19+
// Check if the index exists in the array (to handle sparse arrays)
20+
if (i in array) {
21+
// Call the callback with the current element, index, and array
22+
callback.call(thisArg, array[i], i, array);
23+
}
24+
}
25+
};
26+
}
27+

CodingInterviewQuestions/Polyfill.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// A polyfill is a piece of code (usually JavaScript) that provides functionality that is not natively supported by certain browsers or environments. It's used to ensure that web applications work consistently across different browsers and versions.
2+
3+
// For example, if you're using a new JavaScript feature like Array.prototype.includes that isn't available in older browsers, you can include a polyfill to add this method to those browsers.Here's a simple polyfill for Array.prototype.includes:
4+
5+
6+
if (!Array.prototype.includes) {
7+
Array.prototype.includes = function(searchElement, fromIndex) {
8+
var list = Object(this);
9+
var length = list.length >>> 0;
10+
var i = fromIndex || 0;
11+
12+
if (i < 0) {
13+
i = Math.max(length + i, 0);
14+
}
15+
16+
for (; i < length; i++) {
17+
if (list[i] === searchElement) {
18+
return true;
19+
}
20+
}
21+
22+
return false;
23+
};
24+
}
25+
26+
27+
28+
// This code checks if Array.prototype.includes is already defined. If not, it adds a custom implementation so that the feature is available even in environments where it's not natively supported.
29+
30+
// Polyfills are essential for maintaining compatibility with older browsers and ensuring that modern web applications function correctly across a wide range of environments.
31+
32+
33+
34+
35+
36+
37+
38+
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const people = [
2+
{ name: 'John', age: 25 },
3+
{ name: 'Jane', age: 30 },
4+
{ name: 'Alice', age: 22 },
5+
{ name: 'Bob', age: 28 }
6+
];
7+
8+
// Sorting in ascending order (youngest to oldest)
9+
people.sort((a, b) => a.age - b.age);
10+
11+
console.log('Ascending:', people);

0 commit comments

Comments
 (0)