timoxley / functional-javascript-workshop

A functional javascript workshop. No libraries required (i.e. no underscore), just ES5.
2.06k stars 441 forks source link

Basic: Call exersion - a question about no using `prototype` #210

Open hungdao-testing opened 1 year ago

hungdao-testing commented 1 year ago

Hi all,

I have a solution below, the point is I dont use prototype to parse/convert the arguments (such as. Array.prototype.slice.call(arguments)). So I am wondering this way is suitable to the exercise since I am kind of confusing about the phrase in the task which have a property 'quack' defined directly on them

Task: Write a function duckCount that returns the number of arguments passed to it which have a property 'quack' defined directly on them. Do not match values inherited from prototypes.

My solution

function duckCount() {
    // SOLUTION GOES HERE
    return Object.values(arguments).reduce((prev, curr, index) => {
        if(Object.prototype.hasOwnProperty.call(curr,'quack')){
            prev = prev + 1
        }
        return prev
    }, 0)
  }
gpetrioli commented 9 months ago

Conditions