-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAdvanced Array Manipulations
More file actions
129 lines (116 loc) · 3.94 KB
/
Copy pathAdvanced Array Manipulations
File metadata and controls
129 lines (116 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Remove Last Occurrence of an Element
function removeLastOccurrence(arr, element) {
for (let i = arr.length - 1; i >= 0; i--) {
if (arr[i] === element) {
arr.splice(i, 1);
break;
}
}
return arr;
}
console.log(removeLastOccurrence([3, 5, 3, 1], 3)); // Output: [3, 5, 1]
// Create a Nested Array
function nestArray(arr) {
let result = [];
for (let i = 0; i < arr.length; i += 2) {
result.push(arr.slice(i, i + 2));
}
return result;
}
console.log(nestArray([1, 2, 3, 4, 5, 6])); // Output: [[1, 2], [3, 4], [5, 6]]
console.log(nestArray([1, 2, 3, 4, 5])); // Output: [[1, 2], [3, 4], [5]]
// // Check if All Values Are Even
function allEven(arr) {
return arr.every(num => num % 2 === 0);
}
console.log(allEven([2, 4, 6, 8])); // Output: true
console.log(allEven([2, 3, 4, 6])); // Output: false
Replace Spaces with Plus Signs
function replaceSpaces(str) {
return str.replace(/\s/g, '+');
}
console.log(replaceSpaces("Hello World from JavaScript")); // Output: "Hello+World+from+JavaScript"
// Find the Largest Number in Each Subarray
function largestInSubarrays(arr) {
return arr.map(subArr => Math.max(...subArr));
}
console.log(largestInSubarrays([[10, 5, 2], [25, 8, 7], [1, 2, 3]])); // Output: [10, 25, 3]
// Return the Number of Days in a Month
function getDaysInMonth(month, year) {
return new Date(year, month, 0).getDate();
}
console.log(getDaysInMonth(2, 2020)); // Output: 29 (Leap year)
console.log(getDaysInMonth(2, 2021)); // Output: 28
// Rotate Array to the Right
function rotateRight(arr, k) {
k = k % arr.length; // Handle rotations greater than array length
return arr.slice(-k).concat(arr.slice(0, arr.length - k));
}
console.log(rotateRight([1, 2, 3, 4, 5], 2)); // Output: [4, 5, 1, 2, 3]
// Detect Anagram
function areAnagrams(str1, str2) {
const normalize = str => str.toLowerCase().replace(/[\W_]+/g, '').split('').sort().join('');
return normalize(str1) === normalize(str2);
}
console.log(areAnagrams("listen", "silent")); // Output: true
console.log(areAnagrams("hello", "world")); // Output: false
// Sum of Positive Numbers After Negatives
function sumAfterFirstNegative(arr) {
const negativeIndex = arr.findIndex(num => num < 0);
return arr.slice(negativeIndex + 1).reduce((acc, num) => num > 0 ? acc + num : acc, 0);
}
console.log(sumAfterFirstNegative([1, 2, -3, 4, 5])); // Output: 9
// Random Element from Array
function getRandomElement(arr) {
const randomIndex = Math.floor(Math.random() * arr.length);
return arr[randomIndex];
}
console.log(getRandomElement([1, 2, 3, 4, 5])); // Output: (random element)
// Convert Hex Code to RGB
function hexToRgb(hex) {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
return `rgb(${r}, ${g}, ${b})`;
}
console.log(hexToRgb("#FFFFFF")); // Output: "rgb(255, 255, 255)"
// Validate Palindrome with Removal
function validPalindrome(str) {
let left = 0, right = str.length - 1;
while (left < right) {
if (str[left] !== str[right]) {
return isPalindrome(str, left + 1, right) || isPalindrome(str, left, right - 1);
}
left++;
right--;
}
return true;
}
function isPalindrome(str, left, right) {
while (left < right) {
if (str[left++] !== str[right--]) {
return false;
}
}
return true;
}
console.log(validPalindrome("abca")); // Output: true
console.log(validPalindrome("abc")); // Output: false
// Find Minimum and Maximum in Array
function findMinMax(arr) {
let min = arr[0], max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
return { min, max };
}
console.log(findMinMax([1, 2, 3, 4, 5])); // Output: { min: 1, max: 5 }
console.log(findMinMax([9, 5, 3, 8, 2])); // Output: { min: 2, max: 9 }
// Return Numbers as Words
function numberToWord(num) {
const words = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
return words[num];
}
console.log(numberToWord(5)); // Output: "five"
console.log(numberToWord(0)); // Output: "zero"