简体   繁体   English

使用JavaScript中的每个函数比较数组中的值

[英]Comparing values in an array using a for each function in JavaScript

I have an assignment to perform some specific tasks on an array using a for each function. 我有一个任务,使用for each函数在数组上执行某些特定任务。 I have everything working except for the last one - I'm close but the first and last values being returned are incorrect. 我有一切工作,除了最后一个 - 我很接近,但返回的第一个和最后一个值是不正确的。

In the isNextGreater function I need to take the value of an element in the array and compare it to the next element. 在isNextGreater函数中,我需要获取数组中元素的值并将其与下一个元素进行比较。 If the value of the element is < the value of the next element I need to return a -1. 如果元素的值是<下一个元素的值,我需要返回-1。 If the value is greater I need to return 1. The last element needs to return its original value as there is nothing to compare it to. 如果值更大,我需要返回1.最后一个元素需要返回其原始值,因为没有什么可以比较它。

When the function runs it returns the correct value for [1] and [2] but [0] returns its original value and [3] returns 1. 当函数运行时,它返回[1]和[2]的正确值,但[0]返回其原始值,[3]返回1。

I know I'm close but an missing something! 我知道我很亲密但却缺少一些东西! Could someone give me a hint as to what I am over looking? 有人能给我一个关于我在看什么的暗示吗?

Thanks! 谢谢!

<!doctype html>
<html>
<head>
<title> Functions: forEach </title>
<meta charset="utf-8">
<script>

// 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  

for (var i = 0; i < size; i++) {            
if (theArray[i] < theArray[i+1]){
   theArray[i] = -1;
} else {
   theArray[i] = 1;
}  
}

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);

</script>
</head>
<body>
</body>

Change your isNextGreater function to this, and you are done. isNextGreater函数更改为此,您就完成了。

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);            
} 

Here is a fiddle to check the result (the result are displayed on the console) 是检查结果的小提琴(结果显示在控制台上)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM