0

function filter_Arr(arr) {
  let filters = arr.filter(number => parseInt(number) >= 0)
  console.log(filters)
  return filters;
}

let arr = [0, 1, "1", 2, '2', -3, 3];

filter_Arr(arr);

По моему кодy выводит: [0, 1, "1", 2, '2', 3]

хотя вывод должен быть: [0, 1, 2, 3]

Как правильно отфильтровать массив, чтобы остались только неотрицательные числа?

1 ответ 1

2

function filter_Arr(arr) {  
  return arr.filter(number => typeof number == 'number' && number >= 0);
}

console.log(filter_Arr([0, 1, "1", 2, '2', -3, 3]));

4

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.