Hi. I've been learning javascript for a few days. I got two tasks to solve and I have no idea why they don't work.
First:
function returns an empty array
and second:
functions always returns 0, and in this case it should be 1
Please help me. What's wrong here?
First:
Code:
function filterEvenNumbers(numbers) {
let evenNumbers = [];
for (let i = 0; i < numbers.lenght; i++) {
if (numbers[i] % 2 === 0) {
evenNumbers.push(numbers[i]);
}
}
return evenNumbers;
}
console.log(filterEvenNumbers([1, 2, 3, 4, 5, 6]));
function returns an empty array
and second:
Code:
function countOccurrences (array, element) {
let counter = 0;
for (let i = 0; i < array.lenght; i++) {
if (array[i] === element){
counter++;
}
}
return counter;
}
console.log(countOccurrences([2, 1], 1));
functions always returns 0, and in this case it should be 1
Please help me. What's wrong here?