Home > Back-end >  How to return the same value n amount of times from a closure function call?
How to return the same value n amount of times from a closure function call?

Time:06-06

What I would like to do is have a closure return the same value n amount of times.

let value = getCurrentValue([0, 315, 270, 225, 180, 135, 90], 3);

I should then get:

value() // returns 0
value() // returns 0
value() // returns 0
value() // returns 315
value() // returns 315
value() // returns 315
value() // returns 270
value() // returns 270
value() // returns 270

This is what I tried!

  function getCurrentValue(values, num) {
    let index = -1;
    let l = values.length;

    function increment() {
        index;
      if (index < l) {
        return values[index]
      } else {
        index = -1;
          index;
        return values[index]
      }
    }

    if (num)  {
     for (var i = 0; i < num; i  ){
      return increment         
     }
   }


    return increment;
  }

Any help would be appreciated!

CodePudding user response:

Here is my generator solution & closure solution

// generator solution
function* getCurrentValueIter(iter, times) {
  for (let i = 0; i < iter.length * times; i  ) {
    const cur = Math.floor(i / times);
    yield iter[cur]
  }
}

console.log('generate')
const valueIter = getCurrentValueIter([0, 1], 2)
for (let value of valueIter) {
  console.log(value)
}
// you can also use valueIter manually
// console.log(valueIter.next().value)

// closure solution
function getCurrentValue(iter, times) {
  let i = 0
  function value() {
    while (i < iter.length * times) {
      const cur = Math.floor(i / times);
      i  = 1
      return iter[cur]
    }
  }
  return value
}

const value = getCurrentValue([0, 1], 2)
console.log('closure', value())
console.log(value())
console.log(value())
console.log(value())

CodePudding user response:

Simply use an iterator function.

function* getCurrentValue(arr, count) {
  for (let item of arr) {
    for (let j = 0; j < count; j  ) {
      yield item;
    }
  }
}

for (let value of getCurrentValue([0, 315, 270, 225, 180, 135, 90], 3)) 
{
  console.log(value);
}

CodePudding user response:

Using just a closure - keep track of the index and the number of repeats outside the returned function, so the values persist through calls:

function getCurrentValue(arr, n) {
  let index = 0;
  let repeat = 0;
  
  return function() {
    if (repeat < n) {
      repeat  ;
    } else {
      repeat = 1;
      index  ;
    }
    
    return arr[index];
  }
}

let value = getCurrentValue([0, 315, 270, 225, 180, 135, 90], 3);


console.log(value()); // returns 0
console.log(value()); // returns 0
console.log(value()); // returns 0
console.log(value()); // returns 315
console.log(value()); // returns 315
console.log(value()); // returns 315
console.log(value()); // returns 270
console.log(value()); // returns 270
console.log(value()); // returns 270
console.log(value()); // returns 225
.as-console-wrapper { max-height: 100% !important; }

The if/else section can be shortened by using % to increase the repeat value and cap it to the maximum:

function getCurrentValue(arr, n) {
  let index = -1;
  let repeat = -1;
  
  return function() {
    repeat = (repeat   1) % n;
    if (repeat === 0)
      index  ;
    
    return arr[index];
  }
}

let value = getCurrentValue([0, 315, 270, 225, 180, 135, 90], 3);


console.log(value()); // returns 0
console.log(value()); // returns 0
console.log(value()); // returns 0
console.log(value()); // returns 315
console.log(value()); // returns 315
console.log(value()); // returns 315
console.log(value()); // returns 270
console.log(value()); // returns 270
console.log(value()); // returns 270
console.log(value()); // returns 225
.as-console-wrapper { max-height: 100% !important; }

CodePudding user response:

In Javascript:

const values = [0, 315, 270, 225, 180, 135, 90];
const rNum = 3;

const getCurrentValue = (values, num) => {
    if (num <= 0) {
        return console.log("Repeater is invalid");
    } else {
        values.map((number) => {
            let repeatingCount = 0;
            while (repeatingCount != num) {
                console.log(number);
                repeatingCount  ;
            }
        })
    }
}
getCurrentValue(values, rNum);

CodePudding user response:

const value = getCurrentValue([0, 315, 270, 225, 180, 135, 90], 3);

function getCurrentValue(arr = [], n = 0) {

  // Return if the array is empty or `n` is 0
  if (!arr.length || !n) return;

  let index = 0;
  let count = 0;
  
  return function () {
  
    // Have we reached the end of the array?
    if (index < arr.length) {
      
      // No? Check to see if our count is less than `n`
      // increment it if it is, and log the number
      if (count < n) {
          count;
        console.log(arr[index]);
      
      // Otherwise increase the array index
      // and set `count` to zero
      } else {
          index;
        count = 0;
      }
    
    }
  
  }

}

value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value(),value();

CodePudding user response:

If I understood well this is what you want

    let res = [];
    
    function getCurrentValue(arr, x) {
    res = [];
      for (val of arr) {
        for (let i = 0; i < x; i  ) {
          res.push(val);
        }
      }
    }
    getCurrentValue([0, 42, 4], 3)
  • Related