admin管理员组

文章数量:1187339

Help! I'm being interviewed on Tuesday, including a test on testdome... 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...

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...

Share Improve this question edited Jul 3, 2017 at 1:50 yass 8691 gold badge7 silver badges13 bronze badges asked Jul 2, 2017 at 19:24 AndyAndy 1171 gold badge1 silver badge5 bronze badges 4
  • if(value === undefined) throw new Error('message'); covers both cases. – Emissary Commented Jul 2, 2017 at 19:29
  • 2 typeof value === 'undefined' should work perfectly. Why do you think it didn't? – Bergi 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. – Kapil Raghuwanshi 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. – David Essien Commented Apr 1, 2020 at 15:27
Add a comment  | 

4 Answers 4

Reset to default 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());

 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

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)
}
<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>

本文标签: