11

Help! I'm being interviewed on Tuesday, including a test on testdome.com... I looked at some of their "easy" javascript practise questions, and I'm stumped on this one:

Implement the ensure function so that it throws an error if called without arguments or an argument is undefined. Otherwise it should return the given value.

function ensure(value) {

}

So far, I've got:

function ensure(value) {
  if(value){
    return true;
  }
}

But how do I check if if the function is called "without arguments or an argument is undefined"?

I've tried a few things, like: else if (typeof value === 'undefined'), but that doesn't seem to work...

4
  • if(value === undefined) throw new Error('message'); covers both cases. Commented Jul 2, 2017 at 19:29
  • 2
    typeof value === 'undefined' should work perfectly. Why do you think it didn't? Commented Jul 2, 2017 at 19:40
  • Make sure you took care of ' '. if(typeof value === 'undefined') or if(value === undefined). Both will give correct answer. Commented Aug 14, 2019 at 8:27
  • @Emissary is right. The content of a variable referencing an argument that is not supplied is always undefined. So simply using the strict equality without the quotes added to undefined will do the trick. Commented Apr 1, 2020 at 15:27

4 Answers 4

17

You can check if value === undefined, and if so throw an error. Otherwise return the input value.

function ensure(value) {
  if(value === undefined) {
    throw new Error('no arguments');
  }

  return value;
}

console.log(ensure('text'));
console.log(ensure([0,1,2,3]));
console.log(ensure());

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

2 Comments

So, the new Error code is a built in javascript method? THanks in advance.
@Gel very much in advance, but yes, you're creating a new instance of the built-in Error class.
6
 function noValueException(value) {
       this.value = value;
       this.name = 'noValueException';
    }

    function ensure(value) {
      if(value === undefined){
          throw new noValueException('No argument passed');       
      }else{ return value;}
    }

This will throw an exception to console if no argument is passed to function

1 Comment

@Emissary sorry thought the question was only for no value passed.. corrected my ans now
0

Hey check this code it will definitely works

function ensure(value) {
    if (value === undefined) {
        throw new error("Undefined")
    } else {
        return value;
    }
}
try {
    console.log(ensure("Vikas"));
    console.log(ensure());
} catch (error) {
    console.log(error)
}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0
<script>
    function check(value) {
        if (value === null || value === undefined) {
            throw new Error("undefined")
        }
        else {
            return value
        }
    }
    try {
        check()
    }
    catch (ex) {
        throw new Error(ex);
    }
</script>

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.