What is Wrong with this Code? I should create a function that receives an array of numbers and returns an array containing only the positive numbers. How can it be modified? Especially Modified. Not another code!
all = prompt("Give me an array of numbers seperated by ','");
var splitted = all.split`,`.map(x=>+x);
function returner(splitted){
var positive = [];
for(var i = 0; i < splitted.length; i++);{
var el = splitted[i];
if (el >= 0){
positive.push(el);
}
}
return positive;
}
var positive = returner(splitted);
print(positive);
First I noticed that you are using print
to check your output - that should be console.log()
.
But your real mistake is the semicolon after the for bracket in line 7.
Here is a working code-snippet:
let all = prompt("Give me an array of numbers seperated by ','");
let splitted = all.split`,`.map(x => +x);
function returner(splitted) {
let positive = [];
for (let i = 0; i < splitted.length; i++) {
const el = splitted[i];
if (el >= 0) {
positive.push(el);
}
}
return positive;
}
var positive = returner(splitted);
console.log(positive);