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 |4 Answers
Reset to default 17You 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>
本文标签:
版权声明:本文标题:Javascript function should throw an error if called without arguments or an argument is undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738336622a2077038.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
if(value === undefined) throw new Error('message');
covers both cases. – Emissary Commented Jul 2, 2017 at 19:29typeof value === 'undefined'
should work perfectly. Why do you think it didn't? – Bergi Commented Jul 2, 2017 at 19:40