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 5303274

Browse files
Merge pull request #4 from Vasu7389/feature/new_question
new question added
2 parents c190793 + 35d7eb4 commit 5303274

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

@@ -1549,3 +1549,47 @@ console.log(numbers.reduce((total, num) => total + num));
15491549
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).
15501550
15511551
</details>
1552+
1553+
<details>
1554+
<summary>
1555+
<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>
1556+
1557+
_*You can't use built-in methods like filter or forEach._
1558+
1559+
</summary>
1560+
1561+
**Answer:**
1562+
1563+
Approach 1: Using a loop and conditional statement
1564+
1565+
```js
1566+
function findEvenNumbers(numberArray) {
1567+
const evenNumbers = [];
1568+
for (let i = 0; i < numberArray.length; i++) {
1569+
if (numberArray[i] % 2 === 0) {
1570+
evenNumbers.push(numberArray[i]);
1571+
}
1572+
}
1573+
return evenNumbers;
1574+
}
1575+
```
1576+
1577+
Approach 2: Using recursion and conditional statement
1578+
1579+
```js
1580+
function findEvenNumbersRecursive(numberArray) {
1581+
if (numberArray.length === 0) {
1582+
return [];
1583+
}
1584+
1585+
const firstNumber = numberArray[0];
1586+
const remainingNumbers = numberArray.slice(1);
1587+
1588+
if (firstNumber % 2 === 0) {
1589+
return [firstNumber].concat(findEvenNumbersRecursive(remainingNumbers));
1590+
} else {
1591+
return findEvenNumbersRecursive(remainingNumbers);
1592+
}
1593+
}
1594+
```
1595+
</details>

0 commit comments

Comments
(0)

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