nsuspect
nsuspect

Reputation: 59

Higher-order function

I have an exercise about JavaScript. This exercise requires me to use higher-order functions. I have managed to specify some of the functions so far, but when I try to execute the code, the result does not seem to work properly. I have some images to give you an idea, hopefully, you can help me correct this.

function loop(loops, number, func) {
  var loops = function(n) {
    for (var i = 0; i < n; i++) {
      if (i < 0) {
        console.log('Programme ended')
      }
      if (i > 0) {
        return n;
      }
    }
  }
}

var halve = function(n) {
  return n / 2
}

var square = function(n) {
  return n ** 2;
}

console.log(halve(50));
console.log(loop(5, 200, halve));
console.log(loop(3, 5, square));
console.log(loop(-1, 99, halve));

enter image description here

Upvotes: 0

Views: 744

Answers (3)

Aecrim
Aecrim

Reputation: 18

so first things first: Higher-Order functions are functions that work on other functions. The reason why you get undefined is because you are calling a function which doesn't return anything.

function x(parameter){
  result = parameter + 1;
}
// -> returns undefined every time
console.log(x(5));
// -> undefined
function y(parameter){
  return parameter+1;
}
// -> returns a value that can be used later, for example in console.log
console.log(y(5));
// -> 6

Second, you are using n for your for loop when you should probably use loops so it does the intended code as many times as "loops" indicates instead of the number you insert (i.e. 200, 5, 99).

By having the "console.log" inside a loop you may get a lot of undesired "programme ended" in your output so in my version I kept it out of the loop.

The other two answers given are pretty complete I believe but if you want to keep the for loop here goes:

function loop(loops, number, func){
  if(loops>0){
    for(let i = 0; i< loops; i++){     // let and const are the new ES6 bindings (instead of var)
      number = func(number)
    }
    return number
  }
  else{
    return "Programme ended"
  }
}

function halve(n) {     // maybe it's just me but using function declarations feels cleaner
    return n / 2;
}
  
function square(n) {    
    return n ** 2;
}

console.log(halve(50));
console.log(loop(5, 200, halve));
console.log(loop(3, 5, square));
console.log(loop(-1, 99, halve));

Upvotes: 0

Mulan
Mulan

Reputation: 135415

Your current loop function declares an inner function and then exits. Ie, nothing actually happens -

function loop(loops,number,func){

    // declare loops function
    var loops= function(n){
         // ...
    }

    // exit `loop` function
}

One such fix might be to run the supplied func a number of times in a for loop, like @code_monk suggest. Another option would be to use recursion -

function loop (count, input, func) {
  if (count <= 0)
    return input
  else
    return loop(count - 1, func(input), func)
}

function times10 (num) {
  return num * 10
}

console.log(loop(3, 5, times10))
// 5000

Upvotes: 1

code_monk
code_monk

Reputation: 10148

Here's one way

const loop = (loops, n, fn) => {
  for (let i=0; i<loops; i++) {
    console.log( fn(n) );
  }
};


const halve = (n) => {
  return n / 2;
};

const square = (n) => {
  return n ** 2;
};

loop(2,3,halve);

loop(4,5,square);

Upvotes: 0

Related Questions