Защо Judge ми дава 0/100 на задача 02. Print every N-th Element from an Array?
Ето моето решение: https://pastebin.com/XYKRUd9b
Това е условието на задачата: 2.Print Every N-th Element from an Array
The input comes as two parameters – an array of strings and a number. The second parameter is N – the step.
The output is every element on the N-th step starting from the first one. If the step is 3, you need to return the 1-st, the 4-th, the 7-th … and so on, until you reach the end of the array.
The output is the return value of your function and must be an array.
Example
| Input | Output | 
 | Input | Output | 
 | Input | Output | 
| ['5', '20', '31', '4', '20'], 2 | ['5', '31', '20'] | 
 | ['dsa', 'asd', 'test', 'tset'], 2 
 | ['dsa', 'test'] | ['1', '2', '3', '4', '5'], 6 | ['1'] | 
Hints
- Return all the elements with for loop, incrementing the loop variable with the value of the step variable.
function printEveryNthElementFromAnArray(input) {
let step = Number(input.pop());
input.filter((element, index) => index % step == 0).forEach(element => console.log(element));
}
Ето и друго решение, но пак дава 0 точки.
Функцията ти все още приема само един параметър. Освен това трябва да връща масив:
function printEveryNthElementFromAnArray(input, step) {
return input.filter((element, index) => index % step == 0);
}