Skip to content

Feedback homework JS2 Week 3 #3

@remarcmij

Description

@remarcmij

Hi Jim, here is my feedback on your homework. Unfortunately you do not seem to have taken the trouble of improving your homework with the knowledge obtained from the last lecture.

Step 4.1

This idea was that you call the function that was passed as the parameter parameter inside the function body, rather than accessing it directly. This makes the function foo reusable (although trivial in this case). When you access the bar function directly in foo you can't use foo with some other function you may want it to call, i.e. not without changing the code foo again.

You should also have been warned by the eslint green underline that parameter was unused.

Step 4.2

The functions sayThree and sayFive should only be called when the number is divisible by 3 and 5 respectively. Therefore it should not be necessary to test the numbers again in these functions. A simple console.log as in shown in my solution should have been sufficient. Had you done that, you could also have noted that for the number 15 your code doesn't produce the right result. Although it is divisible by both 3 and 5, your sayFive function is never called in this case.

I added a console.log to prove that:

function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
    for (let i = startIndex; i <= stopIndex; i++) {

        if (i % 3 === 0) {
            sayThree(i);
        }
        else if (i % 5 === 0) {
            sayFive(i);
        }
        else if ((i % 3 === 0) && (i % 5 === 0)) {
            console.log('called');
            sayThree(i);
            sayFive(i);
        }
    }
}

The first problem is that your threeFive function does not use the parameters threeCallback and fiveCallback. Instead you call the sayThree and sayFive functions directly. As explained in the last lecture, this takes away the reusability of your function. It can now not be reused with other callbacks.

The second problem is that your if-else statement is mutually exclusive. If a number is divisible by 3 it will not ever be tested again for divisibility by 5. Again, see my solution how the if statement can be simplified as well as meeting the requirement of the assignment.

Lastly, there was no need to push the numbers to an array in sayThree and sayFive. In fact, the names sayThree and sayFive already suggest that you should 'say' something rather than 'push' something. When we choose names for functions we generally try to use the best descriptive name possible. Conversely, when you see the name of a function such as sayThree you should be able to rely on the assumption that its name accurately reflects its intended purpose.

Steps 4.3

Missing

Step 4.6

Missing

Step 4.7

Missing

Step 5

We discussed this code in a PM. You don't need the += in the code below. A simple + would have been sufficient as we are not using the value of the x parameter any more when we return.

function createBase(val) {
  return function (x) {
    return x += val;
  };
};

Bonus

That was a clever way of solving this problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions