If I have
var numberarr = [1, 2, 3, 4, 5];
How would i make it into
var numberarr2 = [0, 1, 2, 3, 4];
by decrementing 1 from each element?
Find the minimum non-zero element in the array, print it and then subtract this number from all the non-zero elements of the array. If all the elements of the array are < 0, just print 0. Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Java Array Subtract subtractElementwise(int[] a, int[] b) Subtracts the values in the two arrays of integers element-wise.
One way you can do this is to pass the result array as a parameter to the subtract() function. You should, of course, make sure that all arrays are of the same size NUM so that there is no memory access errors. Then you can print all the three arrays in your main() .
You can use .map( )
var numberarr2 = numberarr.map( function(value) {
return value - 1;
} );
Try this:
// Create an array to hold our new values
var numberArr2 = [];
// Iterate through each element in the original array
for(var i = 0; i < numberArr1.length; i++) {
// Decrement the value of the original array and push it to the new one
numberArr2.push(numberArr1[i] - 1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With