I just finished codecademy’s javascript course and have also finished a couple of personal side projects along the way to help reinforce some of what I’ve learned. Now, I’m trying to fill in some gaps that the course leaves out, and that led me to this question on testdome.
“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.”
[CODE]function ensure(value) {
}
This was my first attempt.
[CODE]function ensure(value) {
if (value == null) {
return “error”;
}
else {
return value;
}
}
This passed one of the three criteria, but it’s failing for not returning the correct value if there is no argument or if it’s undefined. After more research, I saw try/catch as a possible solution, but that was not covered in codecademy and my new attempts have even been less successful. I understand the w3schools examples, but I’m failing to fully understand how to apply it in a general real world sense. Before I get too far ahead of myself, though, am I even going down the right path? Or is there something I should be looking towards besides try/catch? Thanks!