How to count the number of elements (tsyfer) how many of them, and positive and negative vste? Given:

var a = [-1 ,-2 ,-3 ,-4 ,-5 , 1 , 2 , 3 , 4 , 5]; var b; for(b=0; b<10 ; b++) { if (a[b]<0) { document.write(a[b]+"<br>") } if (a[b]>0) { document.write(a[b]+"<br>") } } 
  • one
    The fact is that there are no negative numbers in nature. Actually, like positive ones. And the above code displays positive and negative numbers. - KoVadim
  • @KoVadim, there are no positive / negative numbers, but there is a "tsifer")) - Deonis

1 answer 1

Testing here

  var arr = [-1 ,-2 ,-3 ,-4 ,-5 , 1 , 2 , 3 , 4 , 5], positiveN = [], negativeN = []; for(var i = 0; i < arr.length; i++) { arr[i] >= 0 ? positiveN.push(arr[i]) : negativeN.push(arr[i]); } var result = 'Положительные числа: ' + positiveN.join(',') + ' (количество: ' + positiveN.length + ')<br>' + 'Отрицательные числа: ' + negativeN.join(',') + ' (количество: ' + negativeN.length + ')'; document.body.innerHTML = result;