0

How do I create a function that would receive one argument and would print in the console the underlying value of that argument, whether it be a primitive type or a function?

value = function(args){ 
    return args.valueOf() //Only working for line 36
};

var scary = 'boo';
var first = function() { return 'bar'; };
var second = function() {
    return first;
};

var third = function() {
    return second;
};

var nested = function() {
  return function() {
    return function() {
      return function() {
        return function() {
          return function() {
            return function() {
              return function() {
                return function() {
                  return 'super nested';
                };
              };
            };
          };
        };
      };
    };
  };
};


// expected values for all these assessments
console.log(value(scary));   // should be      'boo'
console.log(value(first));   // should be      'bar'
console.log(value(second));  // should also be 'bar'
console.log(value(third));   // should also be 'bar'
console.log(value(nested));  // should be      'super nested'

The value function I made here only gives me the first line for scary and just the functions for the rest, how do I get the values of the other functions?

Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
mrtaz
  • 446
  • 1
  • 6
  • 16
  • Not everybody likes to navigate to external sources for code (+ it's very likely that this link will expire at some time in the future) so you might as well make your code part of the question itself – Icepickle Mar 20 '17 at 22:02
  • 1
    @Icepickle Done :) – Cjmarkham Mar 20 '17 at 22:02

2 Answers2

5

You can check if the args you received has .apply, and if so, reuse your value function with the result of your argument

If not, you return the value

var value = function(args){ 
    if (args && args.apply) {
      return value( args() );
    }
    return args;
};

var scary = 'boo';
var first = function() { return 'bar'; };
var second = function() {
    return first;
};

var third = function() {
    return second;
};

var nested = function() {
  return function() {
    return function() {
      return function() {
        return function() {
          return function() {
            return function() {
              return function() {
                return function() {
                  return 'super nested';
                };
              };
            };
          };
        };
      };
    };
  };
};


// expected values for all these assessments
console.log(value(scary));   // should be      'boo'
console.log(value(first));   // should be      'bar'
console.log(value(second));  // should also be 'bar'
console.log(value(third));   // should also be 'bar'
console.log(value(nested));  // should be      'super nested'

Or you can use a similar method like the one mentioned here to see if it is a function, I most often use the .apply functionality.

As Ibu mentioned in the comments, if you need such a value function, you might need to check your code design. It could be a lot more optimized :)

Community
  • 1
  • 1
Icepickle
  • 12,689
  • 3
  • 34
  • 48
  • 1
    Works wonderfully. However, I would still ask the OP to try to rethink his design. If you have to make so many nested functions you are probably doing something wrong. – Ibu Mar 20 '17 at 22:08
  • @Ibu I would agree :) without a doubt :) – Icepickle Mar 20 '17 at 22:10
  • 1
    @CarlMarkham Maybe, however it was a valid question. What is the diff with helping somebody to do the job they are paid to do, or for their homework? – Icepickle Mar 20 '17 at 22:15
3

Although already answered, another solution would be to check the typeof args

var value = function(args){ 
    if (typeof args === 'function') {
        return value(args.call());
    }
    return args.valueOf() //Only working for line 36
};

var scary = 'boo';
var first = function() { return 'bar'; };
var second = function() {
    return first;
};

var third = function() {
    return second;
};

var nested = function() {
  return function() {
    return function() {
      return function() {
        return function() {
          return function() {
            return function() {
              return function() {
                return function() {
                  return 'super nested';
                };
              };
            };
          };
        };
      };
    };
  };
};


// expected values for all these assessments
console.log(value(scary));   // should be      'boo'
console.log(value(first));   // should be      'bar'
console.log(value(second));  // should also be 'bar'
console.log(value(third));   // should also be 'bar'
console.log(value(nested));  // should be      'super nested'
Cjmarkham
  • 9,484
  • 5
  • 48
  • 81