Skip to content

Commit 4a8732f

Browse files
committed
Arrays Complete Course
1 parent 71172be commit 4a8732f

10 files changed

+201
-0
lines changed

3. Arrays/1. Intro/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const stringArr = ["a", "b", "c", "d"];
2+
const numArr = [1, 2, 3, 4, 5];
3+
const boolArr = [true, false];
4+
const mixed = ["a", 2, true, undefined, null, { a: "a" }, ["b"]];
5+
console.log(mixed);

3. Arrays/2. Custom Array/index.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
class MyArray {
2+
constructor() {
3+
this.length = 0;
4+
this.data = {};
5+
}
6+
7+
push(item) {
8+
this.data[this.length] = item;
9+
this.length++;
10+
return this.length;
11+
}
12+
13+
get(index) {
14+
return this.data[index];
15+
}
16+
17+
pop() {
18+
const lastItem = this.data[this.length - 1];
19+
delete this.data[this.length - 1];
20+
this.length--;
21+
return lastItem;
22+
}
23+
24+
shift() {
25+
const firstItem = this.data[0];
26+
27+
for (let i = 0; i < this.length; i++) {
28+
this.data[i] = this.data[i + 1];
29+
}
30+
31+
// Delete the last element (which now contains the original second element)
32+
delete this.data[this.length - 1];
33+
this.length--;
34+
35+
// Return the first item that was removed from the array
36+
return firstItem;
37+
}
38+
39+
delete(index) {
40+
// Store the item to be removed
41+
const item = this.data[index];
42+
43+
// Shift elements after the target element (excluding the last one)
44+
for (let i = index; i < this.length - 1; i++) {
45+
this.data[i] = this.data[i + 1];
46+
}
47+
48+
// Delete the last element (which now holds the element to be removed)
49+
delete this.data[this.length - 1];
50+
51+
// Decrement length
52+
this.length--;
53+
54+
// Return the removed item
55+
return item;
56+
}
57+
}
58+
59+
const myNewArray = new MyArray();
60+
myNewArray.push("one");
61+
myNewArray.push("two");
62+
myNewArray.push("three");
63+
// myNewArray.pop();
64+
// myNewArray.shift();
65+
myNewArray.delete(1);
66+
// console.log(myNewArray.get(0));
67+
console.log(myNewArray);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// 1. Convert string to array (split method)
2+
// 2. Reverse the array (reverse method)
3+
// 3. Convert array back to string (join method)
4+
5+
const reverseString = (str) => str.split("").reverse().join("");
6+
console.log(reverseString("hello"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// 1. Convert string to array (split method)
2+
// 2. Reverse the array (reverse method)
3+
// 3. Convert array back to string (join method)
4+
// 4. Compare strings
5+
6+
const palindrome = (str) => str.split("").reverse().join("") === str;
7+
8+
console.log(palindrome("cddc"));
9+
console.log(palindrome("Hello"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// 1. Convert number to string (toString method)
2+
// 2. Convert String to array (split method)
3+
// 3. Reverse the string (reverse method)
4+
// 4. Convert array back to string (join method)
5+
// 5. Convert string to number (parseInt method)
6+
// 6. Return the number
7+
8+
const reverseInt = (n) => {
9+
const reversed = n.toString().split("").reverse().join("");
10+
return parseInt(reversed) * Math.sign(n);
11+
};
12+
13+
console.log(reverseInt(-123));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// 1. Make the string lowercase (toLowerCase method)
2+
// 2. Convert string to array (split method)
3+
// 3. Capitalize each word (map method)
4+
// 3. Convert array back to string (join method)
5+
6+
const capitalize = (str) => {
7+
return str
8+
.toLowerCase()
9+
.split(" ")
10+
.map((word) => word[0].toUpperCase() + word.slice(1))
11+
.join(" ");
12+
};
13+
14+
console.log(capitalize("hello world"));
15+
console.log(capitalize("jordan peterson"));
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 1. Print numbers from 1 to n
2+
// 2. If number is divisible by 3, print "Fizz"
3+
// 3. If number is divisible by 5, print "Buzz"
4+
// 4. If number is divisible by 3 and 5, print "FizzBuzz"
5+
// 5. Else, print the number
6+
7+
const fizzBuzz = (n) => {
8+
for (let i = 1; i <= n; i++) {
9+
if (i % 3 === 0 && i % 5 === 0) {
10+
console.log("FizzBuzz");
11+
} else if (i % 3 === 0) {
12+
console.log("Fizz");
13+
} else if (i % 5 === 0) {
14+
console.log("Buzz");
15+
} else {
16+
console.log(i);
17+
}
18+
}
19+
};
20+
21+
fizzBuzz(15);
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const maxProfit = (prices) => {
2+
let minPrice = prices[0]; // Assume the first day is the cheapest to buy
3+
let maxProfit = 0;
4+
5+
for (let i = 1; i < prices.length; i++) {
6+
const currentPrice = prices[i];
7+
// console.log(currentPrice);
8+
9+
// Update minimum price if a lower price is found
10+
minPrice = Math.min(minPrice, currentPrice);
11+
// console.log(minPrice);
12+
13+
// Calculate potential profit for selling today
14+
const potentialProfit = currentPrice - minPrice;
15+
// console.log(potentialProfit);
16+
17+
// Update maxProfit if a higher profit is found
18+
maxProfit = Math.max(maxProfit, potentialProfit);
19+
// console.log(maxProfit);
20+
}
21+
22+
return maxProfit;
23+
};
24+
25+
const prices = [7, 1, 5, 3, 6, 4];
26+
const profit = maxProfit(prices);
27+
console.log("Maximum profit:", profit);
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const chunk = (array, size) => {
2+
const chunked = [];
3+
let index = 0;
4+
5+
while (index < array.length) {
6+
const chunk = array.slice(index, index + size);
7+
// console.log("------------", chunk);
8+
chunked.push(chunk);
9+
index += size;
10+
}
11+
12+
return chunked;
13+
};
14+
15+
// console.log(chunk([1, 2, 3, 4, 5], 2));
16+
console.log(chunk([1, 2, 3, 4, 5, 6, 7, 8], 3));

3. Arrays/3. Challenges/8. Two Sum.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ⚠️ This is not better solution, we'll make it better in the upcoming videos!
2+
3+
function twoSum(nums, target) {
4+
// Loop through each number in the list
5+
for (let i = 0; i < nums.length; i++) {
6+
// For each number, check the rest of the list
7+
for (let j = i + 1; j < nums.length; j++) {
8+
// If the current number and the one we're checking add up to the target, return their indexes
9+
if (nums[i] + nums[j] === target) {
10+
return [i, j];
11+
}
12+
}
13+
}
14+
15+
// If no matching pair is found, return an empty array
16+
return [];
17+
}
18+
19+
const res = twoSum([2, 7, 11, 15], 9);
20+
const res2 = twoSum([1, 3, 7, 9, 2], 11);
21+
console.log(res);
22+
console.log(res2);

0 commit comments

Comments
 (0)