2016-07-27 13 views
0

Im Moment in einem Javascript-Crash-Kurs und bin derzeit auf einem Block von Code stecken. Sie sehen meinen Kommentar in der Funktion makeCountingFunction, um zu sehen, wohin die Logik geht. Kann jemand helfen zu klären, was vor sich geht?Javascript: Zählen der Anzahl der ungeraden Zahlen

Im Code enthalten ist der Lehrer Kommentar.

/** 
* 
* Independent Practice: Functions and Callbacks 
* 
* GOALS 
* 
* Your goal in this exercise is for the last line of code in this file to log 
* the number of odd numbers in the array. 
* 
* DIRECTIONS 
* 
* Implement 'makeCountingFunction()', so that it accepts a predicate function 
* as an argument. 'makeCountingFunction()' should return an anonymous function 
* that is able to: 
* 
* 1. iterate through an array and apply the predicate function to each item in 
*  that array, 
* 2. increment a counter based on the result of applying the predicate function 
*  to that item (i.e. does it match what we're looking for?) 
* 3. return the final count. 
* 
* The predicate function 'isOdd()' should accept an individual number as a 
* parameter and return whether or not that number is odd. 
* 
* BONUS 1: Can you write another predicate function that counts evens? 
* BONUS 2: Can you write a function that will return the sum of all numbers? 
* 
*/ 

function makeCountingFunction(predicate) {  //delcare the function with the parameter predicate 
    return function(list) {      // this returns a second function with the parameter list 
     var count = 0;        // a variable count is declared with the value of 0 
     list.forEach(function(item) {    //this states that for each item of the paramter the code needs to return a third function that has a parameter called item 
     if (predicate(item)) {     // this declares that if the parameter predicate.... has something to do with item???? this is where im lost 
     count++;        // the variable count increments by 1 
    } 
}) 
return count;       // returns the value of count. This gives up the number of odd integers. 
    }; 
} 

function isOdd(a) { 
    return (a % 2) !== 0 
} 

//  ============================================================================= 
// The code below should work without modification. 
//  ============================================================================= 

/** 
* The line below should package up 'makeCountingFunction()' and 'isOdd()' into 
* a single function that accepts an array of items as a parameter, iterates 
* through it and returns a count of how many of those items are odd numbers. 
* This new function is being assigned to the variable 'countTheOdds'. 
*/ 

var countTheOdds = makeCountingFunction(isOdd); 

/** 
* The final line below calls our new 'countTheOdds()' function and passes in an 
* array of numbers. Once your code is working, the line below should return the 
* number 4. 
*/ 

var oddCount = countTheOdds([1, 2, 3, 4, 5, 6, 7]); 
console.log('There are ' + oddCount + ' odd numbers.'); 
// expected output: There are 4 odd numbers. 
+0

'list.forEach (function (item) {' Ihre Analyse dieser Linie ist falsch Sie sind auf jedem einzelnen 'item' des' list' die Funktion aufgerufen wird, nicht eine Funktion der Rückkehr –

+0

Ich denke, du brauchst eine Erklärung, also welcher Teil ist Unschärfe? –

+0

Ich lief deinen Code in der Konsole und es hat gut funktioniert, was ist das Problem? –

Antwort

-1

list.forEach(function(item) { Ihre Analyse dieser Linie ist falsch. Sie rufen die Funktion für jede einzelne item der list auf, die keine Funktion zurückgibt. In der folgenden Zeile, in der Sie Ihren verlorenen Wert angeben, denken Sie daran, dass predicate eine -Funktion ist, in diesem Fall, wenn die Prädikatfunktion für item von list den Wert true zurückgibt, den Zähler inkrementieren.

Es sieht so aus, als ob Ihre Aufgabe ist, die Prädikatfunktion in Frage zu schreiben, die für ungerade und falsche für gerade zurückgibt, dann die bereitgestellte Funktion darauf aufruft, die zurückgegebene Funktion annimmt und sie mit einer Liste von Zahlen aufruft, um sie zu verifizieren funktioniert:..

makeCountingFunction(thePredicateYouWrite)([1,2,3,4,5]); // should be 3 
+0

Hier ist eine Antwort, die ich auf Facebook erhalten habe, könnte helfen. http://imgur.com/a/8xZpF – dosebot

Verwandte Themen