2016-03-02 75 views
0

我編寫了一個JavaScript函數來查找奇數整數,負整數,平均值和中值。如何返回對象而不是控制檯日誌值

該代碼正在完成我想要的所有內容,但是,我想知道是否可以重寫它以使函數返回對象而不僅僅是控制檯日誌值。我還包括一個鏈接到我的JS斌(https://jsbin.com/digagozuru/edit?html,css,js,console,output

任何意見或建議,將不勝感激!謝謝。

var arrayAnalyzer = function(myArray) { 

    var odds = 0; 
    var negatives = 0; 
    var avg; 
    var median; 

    for (var i = 0; i < myArray.length; i++) { 
     if (myArray[i] % 2 !== 0) { 
     odds += 1; 
     } 
     if (myArray[i] < 0) { 
     negatives += 1; 
     } 
    } 

    console.log("There are " + odds + " odd numbers."); 
    console.log("There are " + negatives + " negative numbers."); 

    var sum = myArray.reduce(function(previousValue, currentValue) { 
     return previousValue + currentValue; 
    }); 

    avg = sum/myArray.length; 
    console.log("The average is " + avg.toFixed(2)); 

    var orderedArray = myArray.sort(function(a, b) { 
     return a - b; 
    }); 

    if (orderedArray.length % 2 === 0) { 
     var position1 = orderedArray.length/2; 
     var position2 = position1 - 1; 
     median = (orderedArray[position1] + orderedArray[position2])/2; 
    } else { 
     var position = Math.floor(orderedArray.length/2); 
     median = orderedArray[position]; 
    } 

    console.log("The median is " + median); 
    }; 

    arrayAnalyzer([7, -3, 0, 12, 44, -5, 3]); 

回答

2
var arrayAnalyzer = function(myArray) { 

    var odds = 0; 
    var negatives = 0; 
    var avg; 
    var median; 

    for (var i = 0; i < myArray.length; i++) { 
    if (myArray[i] % 2 !== 0) { 
     odds += 1; 
    } 
    if (myArray[i] < 0) { 
     negatives += 1; 
    } 
    } 

    console.log("There are " + odds + " odd numbers."); 
    console.log("There are " + negatives + " negative numbers."); 

    var sum = myArray.reduce(function(previousValue, currentValue) { 
    return previousValue + currentValue; 
    }); 

    avg = sum/myArray.length; 
    console.log("The average is " + avg.toFixed(2)); 

    var orderedArray = myArray.sort(function(a, b) { 
    return a - b; 
    }); 

    if (orderedArray.length % 2 === 0) { 
    var position1 = orderedArray.length/2; 
    var position2 = position1 - 1; 
    median = (orderedArray[position1] + orderedArray[position2])/2; 
    } else { 
    var position = Math.floor(orderedArray.length/2); 
    median = orderedArray[position]; 
    } 

    console.log("The median is " + median); 
    // Returns an object with named attributes 
    return { 
    odds:odds, 
    negatives:negatives, 
    avg:avg, 
    median:median 
    }; 
}; 

var myArray = arrayAnalyzer([7, -3, 0, 12, 44, -5, 3]); 
console.log("Odds: " + myArray.odds + 
    "\nNegatives: " + myArray.negatives + 
    "\nAverage:" + myArray.avg + 
    "\nMedian: " + myArray.median); 
+0

謝謝,這對我有意義。我還是JavaScript的新手。你能幫我弄清楚我的代碼應該放在哪裏嗎?再次感謝。 –

+0

編輯顯示將它放在給定代碼中的位置。該函數獲取所有計算並將它們放入返回的對象中。 – Nielsvh

+0

謝謝!您的意見非常有幫助。 –

1

我不知道如果我得到你的問題的權利,但你爲什麼不以你的方法開始創建一個對象,然後,一旦你計算出它們寫在對象中的值?

var arrayAnalyzer = function(myArray) { 
 

 
    var odds = 0; 
 
    var negatives = 0; 
 
    var avg; 
 
    var median; 
 
    var result = {}; 
 

 
    for (var i = 0; i < myArray.length; i++) { 
 
    if (myArray[i] % 2 !== 0) { 
 
     odds += 1; 
 
    } 
 
    if (myArray[i] < 0) { 
 
     negatives += 1; 
 
    } 
 
    } 
 

 
    result.negatives = negatives; 
 
    result.odds = odds; 
 

 
    var sum = myArray.reduce(function(previousValue, currentValue) { 
 
    return previousValue + currentValue; 
 
    }); 
 

 
    avg = sum/myArray.length; 
 
    result.avg = avg; 
 

 
    var orderedArray = myArray.sort(function(a, b) { 
 
    return a - b; 
 
    }); 
 

 
    if (orderedArray.length % 2 === 0) { 
 
    var position1 = orderedArray.length/2; 
 
    var position2 = position1 - 1; 
 
    median = (orderedArray[position1] + orderedArray[position2])/2; 
 
    } else { 
 
    var position = Math.floor(orderedArray.length/2); 
 
    median = orderedArray[position]; 
 
    } 
 

 
    result.median = median; 
 
    return result; 
 
}; 
 
console.log(arrayAnalyzer([7, -3, 0, 12, 44, -5, 3]));

+0

這似乎是工作,但我現在得「的翻譯:」在我的輸出不必要的加法。 –

+0

這是因爲您正在使用控制檯。返回的對象與我的代碼中的完全相同,但構造方式不同。 – Nielsvh

相關問題