Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 35d7eb4

Browse files
new question added
1 parent 6bf6da5 commit 35d7eb4

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

‎README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: "Prepare for your next 2024 JavaScript interview with these tricky
44
githubPath: "https://github.com/Vasu7389/JavaScript-Interview-Questions"
55
---
66

7-
<span style=" font-size: 1rem; border-bottom: 1px solid grey;"> Updated Jan 07, 2024 </span>
7+
<span style=" font-size: 1rem; border-bottom: 1px solid grey;"> Updated Mar 31, 2024 </span>
88

99
In this article, we will cover a range of JavaScript interview questions, including those related to the latest versions of the language (ES6, ES7, ES8, and ES9).
1010

@@ -1551,3 +1551,47 @@ console.log(numbers.reduce((total, num) => total + num));
15511551
4. **Output:** The final `console.log()` statement outputs the result of the `reduce()` operation, which is 30 (the sum of all the doubled numbers in the array).
15521552
15531553
</details>
1554+
1555+
<details>
1556+
<summary>
1557+
<h3>47. Write a JavaScript function that takes an array of numbers as input and returns a new array containing only the even numbers.</h3>
1558+
1559+
_*You can't use built-in methods like filter or forEach._
1560+
1561+
</summary>
1562+
1563+
**Answer:**
1564+
1565+
Approach 1: Using a loop and conditional statement
1566+
1567+
```js
1568+
function findEvenNumbers(numberArray) {
1569+
const evenNumbers = [];
1570+
for (let i = 0; i < numberArray.length; i++) {
1571+
if (numberArray[i] % 2 === 0) {
1572+
evenNumbers.push(numberArray[i]);
1573+
}
1574+
}
1575+
return evenNumbers;
1576+
}
1577+
```
1578+
1579+
Approach 2: Using recursion and conditional statement
1580+
1581+
```js
1582+
function findEvenNumbersRecursive(numberArray) {
1583+
if (numberArray.length === 0) {
1584+
return [];
1585+
}
1586+
1587+
const firstNumber = numberArray[0];
1588+
const remainingNumbers = numberArray.slice(1);
1589+
1590+
if (firstNumber % 2 === 0) {
1591+
return [firstNumber].concat(findEvenNumbersRecursive(remainingNumbers));
1592+
} else {
1593+
return findEvenNumbersRecursive(remainingNumbers);
1594+
}
1595+
}
1596+
```
1597+
</details>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /