0

Given an array, return one value at a time with each function call. I need to make a function where I'm given an array of items and I need to return one value at a time until the last value.

I looked in on some answers and this is something that I thought would work but it's an infinite loop.

Am I close to a solution? Where am I going wrong?

function functionName() {
  
  var vals =
  ["shot_type","shot_height","shot_angle","framed","scene_depth"];
  for(var i=0; i<vals.length; i++) {
    functionName(vals[i]);
  }
}

functionName(); //expect 'shot type'
functionName(); //expect 'shot height'

2
  • you keep calling functionName. Commented Jan 18, 2018 at 21:00
  • 1
    you should look at iterators. Commented Jan 18, 2018 at 21:00

4 Answers 4

2

You could use iterators, but as I don't know about those I'd use a closure.

let functionName = () => {
  let i = -1;
  var vals = ["shot_type","shot_height","shot_angle","framed","scene_depth"];
  
  functionName = () => {
    i++;
    return vals[i];
  }
  
  return functionName();
}

console.log(functionName());
console.log(functionName());
console.log(functionName());

Here i is defined above the scope of the newly redefined functionName, and this is the reference to i that the new function retains.

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

5 Comments

Hey, I like this solution but the snippet is skipping the first item? I notice that it is returning "shot_height" first, when it should be "shot_type"?
My bad, i needs to start at -1 as we're incrementing before the return. I've updated my answer.
Hmm, why not start at 0 and then do return vals[i++] instead of incrementing on the previous line?
Sure, that would work but I like to be verbose. Makes it easier for others to understand!
Pretty good, the inner function being an arrow function keeps the same scope
0
  const functionName = (iter => () => iter.next().value)(vals.values());

This builds up an iterator and closures it in an IIFE, so you can iterate over it.

Comments

0

Not 100% sure how you want to "start over" but I went back to the top/0 one. Closure then call a function to get the value from that. Assumes at least 1.

var functionName = function() {
  var whichone = -1;
  var vals = ["shot_type", "shot_height", "shot_angle", "framed", "scene_depth"];
  this.getvalue = function() {
    whichone++;
    if (whichone === vals.length) {
      whichone = 0;
    }
    return vals[whichone];
  };
  return this;
}();

console.log(functionName.getvalue()); //expect 'shot type'
console.log(functionName.getvalue()); //expect 'shot height'
console.log(functionName.getvalue());// angle
console.log(functionName.getvalue());// framed
console.log(functionName.getvalue());//scene_depth
console.log(functionName.getvalue());// back to shot type

Comments

-1

Arrays are iterators already, so if you can use iterators (es6+), then you can just do:

var vals = ["shot_type","shot_height","shot_angle","framed","scene_depth"];
const iter = vals[Symbol.iterator]();

console.log(iter.next().value);
console.log(iter.next().value);
console.log(iter.next().value);
// And so on.

// The iter.next() method returns an object: {value: any, done: boolean};
// When done is true, then there are no more values to get from the iterator.

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.