Javascript Array Question 25

Introduction

Zero Out Negative Numbers

Set negative array values to zero.

function zero_out_negative(arr) {
  //code here
  return arr;
}
zero_out_negative([-1,2,3,-5]);


function zero_out_negative(arr) {
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] < 0) {
      arr[i] = 0;
    }
  }
  return arr;
}
zero_out_negative([-1,2,3,-5]);



PreviousNext

Related