JSFiddle - React, Tailwind, and code Playground
JavaScript
// the zeros array
var zeros = [0, 0, 0, 0];
// your code here
//Function to display the contents of the Array.
function showArray (value, index, theArray) {
console.log("Array[" + index + "]" + ":" + value);
}
//Function to assign random values to the passed array
function makeArrayRandom(value, index, theArray) {
var maxSize = 5;
var randomNum = Math.floor(Math.random() * maxSize);
theArray[index] = randomNum;
console.log("Array[" + index + "]" + ":" + randomNum);
}
//Function to create a copy of the random numbers array
function map(value, index, theArray) {
var arrayCopy = [];
arrayCopy[index] = theArray[index];
console.log("Array[" + index + "]" + ":" + value);
return arrayCopy;
}
//Function to compare the values of the array
function isNextGreater(value, index, theArray) {
var size = theArray.length
if (index < size - 1){
if (value < theArray[index+1] ){
value = -1;
} else {
value = 1;
}
}else{
value = value;
}
console.log("Array[" + index + "]" + ":" + value);
}
//Use ForEach to pass Array data to functions.
console.log("Display the Array:");
zeros.forEach(showArray);
console.log("Random Array:");
zeros.forEach(makeArrayRandom);
console.log("Copy of Zeros:");
zeros.forEach(map);
console.log("Is Next Greater:");
zeros.forEach(isNextGreater);