5

It's the test task on the job.

A farmer has rabbits. Each rabbit is its weight. When the time comes, he kills them function cut(rabbit). You must write a function of cut so that it had the appearance of cut(rabbit1)(rabbit2)...(rabbitN) and deduced the total mass and the number of rabbits.

For example:

var rabbit1 = {weight: 5},
    rabbit2 = {weight: 4};

console.log(cut(rabbit1)(rabbit2));

In the console we will see "9 kg of rabbits or 2 pieces".

JSFiddle - https://jsfiddle.net/sjao7ut8/

How I can write function cut()?

7
  • @BlazeSahlzen I don't know how I can pass parameters in function like this. Commented Jun 6, 2016 at 9:06
  • If you need more than one parameter (rabbit1, ...., rabbitn), you just pass all the rabbits you want. Javascript functions don't care Commented Jun 6, 2016 at 9:08
  • 1
    why dont you use array rabbits instead of rabbit1, rabbit2, and so on ? and also show us what you've done so far. Commented Jun 6, 2016 at 9:08
  • 2
    You need to research function currying. I wouldn't be surprised if you need to have an empty set of parentheses at the end too, ie console.log(cut(rabbit1)(rabbit2)()); Commented Jun 6, 2016 at 9:08
  • 1
    If I understand the words "test task on the job" correctly, you have applied for a job which obviously wants you to have a certain level of javascript skills. And then you say you "don't know how you can pass parameters in function like this"? Commented Jun 6, 2016 at 9:12

4 Answers 4

7

You could use chaining, a fluent interface, which returns the function you called until the environment requires a primitive value.

function cut(rabbit) {
    var weight = 0,
        count = 0,
        fn = function (o) {
            weight += o.weight;
            count++;
            return fn;
        };

    fn.toString = function () {
        return weight + ' kg of rabbits or ' + count + ' piece' + (count > 1 ? 's' : '');
    }

    return fn(rabbit);
}

var rabbit1 = { weight: 5 },
    rabbit2 = { weight: 4 };
    
console.log(cut(rabbit1)(rabbit2));

Sign up to request clarification or add additional context in comments.

3 Comments

In the chrome dev console this prints and extra function before the output. Can anything be done about that?
@BlazeSahlzen, you can prepend an empty string, like console.log('' + cut(rabbit1)(rabbit2));
@JustusRomijn, yes, because the function is retuned
0

what you need to do here is to use recursive call, which returns a function and uses arguments.callee

something like this should do the job:

function cut(firstRabbit) {
    var mass = firstRabbit.weight;
    return function(nextRabbit) {
        if (typeof nextRabbit !== 'undefined') {
            mass += nextRabbit.weight;
            return arguments.callee;
        } else {
            return mass;
        }
    };
}

Example usage (remember to add () at the end):

var rabbit1 = {weight: 5},
rabbit2 = {weight: 4};

console.log(cut(rabbit1)(rabbit2)());

Comments

0

You can try following code:

var farmer = { 
    cut: function(rabits) { 
    var cutWeight = 10;
    var totalCuts = 0;
    var totalWeight = 0;
    var count = rabits.length;

    for(var i=0; i < count; i++) {
        var rabit = rabits[i];

      if(rabit.weight >= cutWeight) {
        totalWeight += rabit.weight;
        if(totalCuts === 0) {
            totalCuts = 1;
        }
        else {
            totalCuts += 1;
        }
      }
    }

    console.log(totalWeight + "Kg of Rabits and " + totalCuts + "cuts.")
  }
};

farmer.cut([{weight: 11}, {weight: 5}, {weight: 13}]); // Call the function

Hope this will help you.

Comments

-1
Function.prototype.toString = function() {
 return Function.prototype.rabbitsWeight + "кг кроликов или " + Function.prototype.rabbitsCount + " штук";
}; 

Function.prototype.rabbitsWeight = 0;
Function.prototype.rabbitsCount = 0;

window.rabbitsCut = eval("new Function('rabbit', 'Function.prototype.rabbitsWeight += rabbit.weight; ++Function.prototype.rabbitsCount; return eval(window.rabbitsCut);');");

var cut = window.rabbitsCut;

console.log(cut({weight: 5})({weight: 4}));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.