## JavaScript Array Methods:
### Easy Questions
1. **Using `map` to Double Elements**
- **Question**: Write a function that uses the `map` method to create a new array where each element is doubled.
- **Sample Input**:
```javascript
array = [1, 2, 3, 4]
```
- **Sample Output**:
```javascript
[2, 4, 6, 8]
```
- **Explanation**: The `map` method creates a new array with the results of calling a provided function on every element.
2. **Using `filter` to Select Positive Numbers**
- **Question**: Write a function that uses the `filter` method to create a new array containing only positive numbers from the input array.
- **Sample Input**:
```javascript
array = [-1, 0, 1, 2]
```
- **Sample Output**:
```javascript
[1, 2]
```
- **Explanation**: The `filter` method creates a new array with all elements that pass the test implemented by the provided function.
3. **Using `reduce` to Sum Elements**
- **Question**: Write a function that uses the `reduce` method to compute the sum of all elements in an array.
- **Sample Input**:
```javascript
array = [1, 2, 3, 4]
```
- **Sample Output**:
```javascript
10
```
- **Explanation**: The `reduce` method applies a function against an accumulator and each element to reduce the array to a single value.
4. **Using `includes` to Check for Element**
- **Question**: Write a function that checks if a given element exists in an array using the `includes` method and returns a boolean.
- **Sample Input**:
```javascript
array = [1, 2, 3, 4], element = 3
```
- **Sample Output**:
```javascript
true
```
- **Explanation**: The `includes` method checks if an array contains a specific element and returns `true` or `false`.
5. **Using `length` to Get Array Size**
- **Question**: Write a function that returns the number of elements in an array using the `length` property.
- **Sample Input**:
```javascript
array = [1, 2, 3, 4, 5]
```
- **Sample Output**:
```javascript
5
```
- **Explanation**: The `length` property returns the number of elements in an array.
6. **Using `push` to Add Elements**
- **Question**: Write a function that takes an array and an element as input and uses the `push` method to add the element to the end of the array. Return the modified array.
- **Sample Input**:
```javascript
array = [1, 2, 3], element = 4
```
- **Sample Output**:
```javascript
[1, 2, 3, 4]
```
- **Explanation**: The `push` method adds one or more elements to the end of an array.
7. **Using `pop` to Remove Last Element**
- **Question**: Write a function that removes the last element from an array using the `pop` method and returns the modified array.
- **Sample Input**:
```javascript
array = [1, 2, 3, 4]
```
- **Sample Output**:
```javascript
[1, 2, 3]
```
- **Explanation**: The `pop` method removes the last element from an array and returns that element.
8. **Using `concat` to Merge Arrays**
- **Question**: Write a function that merges two arrays using the `concat` method and returns the new array.
- **Sample Input**:
```javascript
array1 = [1, 2], array2 = [3, 4]
```
- **Sample Output**:
```javascript
[1, 2, 3, 4]
```
- **Explanation**: The `concat` method creates a new array by merging two or more arrays without modifying the original arrays.
9. **Using `join` to Convert Array to String**
- **Question**: Write a function that converts an array into a string with elements separated by a comma using the `join` method.
- **Sample Input**:
```javascript
array = [1, 2, 3, 4]
```
- **Sample Output**:
```javascript
"1,2,3,4"
```
- **Explanation**: The `join` method joins all elements of an array into a string, with an optional separator (default is a comma).
### Medium Questions
10. **Using `map` to Transform Strings**
- **Question**: Write a function that uses the `map` method to create a new array where each element (a number) is converted to a string with a dollar sign prefix.
- **Sample Input**:
```javascript
array = [1, 2, 3]
```
- **Sample Output**:
```javascript
["$1", "$2", "$3"]
```
- **Explanation**: The `map` method transforms each element into a new value, here adding a `$` prefix to each number.
11. **Using `filter` to Select Elements by Condition**
- **Question**: Write a function that uses the `filter` method to create a new array containing only elements greater than a given threshold.
- **Sample Input**:
```javascript
array = [1, 5, 2, 8, 3], threshold = 4
```
- **Sample Output**:
```javascript
[5, 8]
```
- **Explanation**: The `filter` method selects elements that satisfy the provided condition.
12. **Using `reduce` to Compute Maximum**
- **Question**: Write a function that uses the `reduce` method to find the maximum value in an array.
- **Sample Input**:
```javascript
array = [3, 7, 2, 9, 1]
```
- **Sample Output**:
```javascript
9
```
- **Explanation**: The `reduce` method compares each element to find the maximum value.
13. **Using `forEach` to Modify Elements**
- **Question**: Write a function that uses the `forEach` method to triple each element in an array and returns the modified array.
- **Sample Input**:
```javascript
array = [1, 2, 3]
```
- **Sample Output**:
```javascript
[3, 6, 9]
```
- **Explanation**: The `forEach` method executes a function for each element, modifying the array in place.
14. **Using `find` to Get First Match**
- **Question**: Write a function that uses the `find` method to return the first even number in an array.
- **Sample Input**:
```javascript
array = [1, 3, 4, 6, 7]
```
- **Sample Output**:
```javascript
4
```
- **Explanation**: The `find` method returns the first element that satisfies the provided condition, or `undefined` if none is found.
15. **Using `some` to Check Condition**
- **Question**: Write a function that uses the `some` method to check if an array contains at least one negative number.
- **Sample Input**:
```javascript
array = [1, 2, -3, 4]
```
- **Sample Output**:
```javascript
true
```
- **Explanation**: The `some` method tests whether at least one element passes the provided test.
16. **Using `every` to Verify Condition**
- **Question**: Write a function that uses the `every` method to check if all elements in an array are even numbers.
- **Sample Input**:
```javascript
array = [2, 4, 6, 8]
```
- **Sample Output**:
```javascript
true
```
- **Explanation**: The `every` method tests whether all elements pass the provided test.
17. **Using `slice` to Extract Subarray**
- **Question**: Write a function that extracts a portion of an array between two indices (inclusive of start, exclusive of end) using the `slice` method.
- **Sample Input**:
```javascript
array = [1, 2, 3, 4, 5], start = 1, end = 4
```
- **Sample Output**:
```javascript
[2, 3, 4]
```
- **Explanation**: The `slice` method returns a shallow copy of a portion of an array without modifying the original.
18. **Using `map` with Index**
- **Question**: Write a function that uses the `map` method to create a new array where each element is a string describing its index and value (e.g., "Index 0: 1").
- **Sample Input**:
```javascript
array = [1, 2, 3]
```
- **Sample Output**:
```javascript
["Index 0: 1", "Index 1: 2", "Index 2: 3"]
```
- **Explanation**: The `map` method can use the index parameter to include position information in the transformation.
### Hard Questions
19. **Using `reduce` to Group Elements**
- **Question**: Write a function that uses the `reduce` method to group array elements by their value (counting occurrences) and return an object with values as keys and counts as values.
- **Sample Input**:
```javascript
array = [1, 2, 2, 3, 1]
```
- **Sample Output**:
```javascript
{ "1": 2, "2": 2, "3": 1 }
```
- **Explanation**: The `reduce` method builds an object by accumulating counts of each value.
20. **Using `flatMap` with `filter`**
- **Question**: Write a function that uses `flatMap` to create a new array where each even number in the input array is transformed into an array of that number repeated twice, and odd numbers are filtered out.
- **Sample Input**:
```javascript
array = [1, 2, 3, 4]
```
- **Sample Output**:
```javascript
[2, 2, 4, 4]
```
- **Explanation**: The `flatMap` method maps even numbers to arrays and flattens the result, effectively filtering out odd numbers.
---
## JavaScript String Methods: 20 Questions
### Easy Questions
1. **Using `length` to Get String Length**
- **Question**: Write a function that returns the length of a given string using the `length` property.
- **Sample Input**:
```javascript
str = "hello"
```
- **Sample Output**:
```javascript
5
```
- **Explanation**: The `length` property returns the number of characters in a string.
2. **Using `toUpperCase` to Convert to Uppercase**
- **Question**: Write a function that converts a string to uppercase using the `toUpperCase` method.
- **Sample Input**:
```javascript
str = "hello"
```
- **Sample Output**:
```javascript
"HELLO"
```
- **Explanation**: The `toUpperCase` method returns a new string with all characters converted to uppercase.
3. **Using `toLowerCase` to Convert to Lowercase**
- **Question**: Write a function that converts a string to lowercase using the `toLowerCase` method.
- **Sample Input**:
```javascript
str = "HELLO"
```
- **Sample Output**:
```javascript
"hello"
```
- **Explanation**: The `toLowerCase` method returns a new string with all characters converted to lowercase.
4. **Using `charAt` to Get Character**
- **Question**: Write a function that returns the character at a specified index in a string using the `charAt` method.
- **Sample Input**:
```javascript
str = "hello", index = 1
```
- **Sample Output**:
```javascript
"e"
```
- **Explanation**: The `charAt` method returns the character at the specified index.
5. **Using `includes` to Check Substring**
- **Question**: Write a function that checks if a substring exists within a string using the `includes` method and returns a boolean.
- **Sample Input**:
```javascript
str = "hello world", substr = "world"
```
- **Sample Output**:
```javascript
true
```
- **Explanation**: The `includes` method checks if a string contains a specified substring.
6. **Using `indexOf` to Find Substring**
- **Question**: Write a function that finds the index of the first occurrence of a substring in a string using the `indexOf` method. Return -1 if not found.
- **Sample Input**:
```javascript
str = "hello world", substr = "world"
```
- **Sample Output**:
```javascript
6
```
- **Explanation**: The `indexOf` method returns the index of the first occurrence of a substring, or -1 if not found.
7. **Using `slice` to Extract Substring**
- **Question**: Write a function that extracts a portion of a string between two indices (inclusive of start, exclusive of end) using the `slice` method.
- **Sample Input**:
```javascript
str = "hello world", start = 0, end = 5
```
- **Sample Output**:
```javascript
"hello"
```
- **Explanation**: The `slice` method extracts a portion of a string and returns it as a new string.
8. **Using `trim` to Remove Whitespace**
- **Question**: Write a function that removes leading and trailing whitespace from a string using the `trim` method.
- **Sample Input**:
```javascript
str = " hello "
```
- **Sample Output**:
```javascript
"hello"
```
- **Explanation**: The `trim` method removes whitespace from both ends of a string.
9. **Using `concat` to Combine Strings**
- **Question**: Write a function that combines two strings using the `concat` method.
- **Sample Input**:
```javascript
str1 = "hello", str2 = " world"
```
- **Sample Output**:
```javascript
"hello world"
```
- **Explanation**: The `concat` method combines two or more strings and returns a new string.
### Medium Questions
10. **Using `split` and `map` to Transform Words**
- **Question**: Write a function that splits a string into words and uses `map` to capitalize the first letter of each word.
- **Sample Input**:
```javascript
str = "hello world"
```
- **Sample Output**:
```javascript
["Hello", "World"]
```
- **Explanation**: The `split` method creates an array of words, and `map` transforms each word by capitalizing its first letter.
11. **Using `replace` to Replace Substring**
- **Question**: Write a function that replaces the first occurrence of a substring with a new string using the `replace` method.
- **Sample Input**:
```javascript
str = "hello world", oldStr = "world", newStr = "everyone"
```
- **Sample Output**:
```javascript
"hello everyone"
```
- **Explanation**: The `replace` method replaces the first occurrence of a substring with a new string.
12. **Using `startsWith` to Check Prefix**
- **Question**: Write a function that checks if a string starts with a specified prefix using the `startsWith` method.
- **Sample Input**:
```javascript
str = "hello world", prefix = "hello"
```
- **Sample Output**:
```javascript
true
```
- **Explanation**: The `startsWith` method checks if a string begins with a specified substring.
13. **Using `endsWith` to Check Suffix**
- **Question**: Write a function that checks if a string ends with a specified suffix using the `endsWith` method.
- **Sample Input**:
```javascript
str = "hello world", suffix = "world"
```
- **Sample Output**:
```javascript
true
```
- **Explanation**: The `endsWith` method checks if a string ends with a specified substring.
14. **Using `repeat` to Repeat String**
- **Question**: Write a function that repeats a string a specified number of times using the `repeat` method.
- **Sample Input**:
```javascript
str = "abc", count = 3
```
- **Sample Output**:
```javascript
"abcabcabc"
```
- **Explanation**: The `repeat` method returns a new string with the original string repeated the specified number of times.
15. **Using `padStart` to Pad String**
- **Question**: Write a function that pads the start of a string with a specified character until it reaches a target length using the `padStart` method.
- **Sample Input**:
```javascript
str = "5", targetLength = 3, padChar = "0"
```
- **Sample Output**:
```javascript
"005"
```
- **Explanation**: The `padStart` method pads the start of a string with a specified character.
16. **Using `padEnd` to Pad String**
- **Question**: Write a function that pads the end of a string with a specified character until it reaches a target length using the `padEnd` method.
- **Sample Input**:
```javascript
str = "5", targetLength = 3, padChar = "0"
```
- **Sample Output**:
```javascript
"500"
```
- **Explanation**: The `padEnd` method pads the end of a string with a specified character.
17. **Using `lastIndexOf` to Find Last Occurrence**
- **Question**: Write a function that finds the index of the last occurrence of a substring in a string using the `lastIndexOf` method. Return -1 if not found.
- **Sample Input**:
```javascript
str = "hello hello", substr = "hello"
```
- **Sample Output**:
```javascript
6
```
- **Explanation**: The `lastIndexOf` method returns the index of the last occurrence of a substring.
18. **Using `substring` to Extract Substring**
- **Question**: Write a function that extracts a substring between two indices (inclusive of start, exclusive of end) using the `substring` method.
- **Sample Input**:
```javascript
str = "hello world", start = 6, end = 11
```
- **Sample Output**:
```javascript
"world"
```
- **Explanation**: The `substring` method extracts characters between two indices.
### Hard Questions
19. **Using `replaceAll` for Global Replacement**
- **Question**: Write a function that replaces all occurrences of a substring with a new string using the `replaceAll` method.
- **Sample Input**:
```javascript
str = "hello hello hello", oldStr = "hello", newStr = "hi"
```
- **Sample Output**:
```javascript
"hi hi hi"
```
- **Explanation**: The `replaceAll` method replaces all occurrences of a substring, introduced in ES2021.
20. **Using `match` with Regular Expression**
- **Question**: Write a function that uses the `match` method with a regular expression to find all vowels in a string and returns them as an array.
- **Sample Input**:
```javascript
str = "hello world"
```
- **Sample Output**:
```javascript
["e", "o", "o"]
```
- **Explanation**: The `match` method retrieves all matches of a regular expression (`/[aeiou]/g`) in a string.
---