Now let's say we want to make sure that the caller always gets a numeric value back, even if the input wasn't of the right type.
To achieve that, we need to check the type of the argument that was passed in. If it wasn't a number, we probably shouldn't try to multiply it. We'll just return
var cube = function (x) {
if (typeof(x) !== 'number') return 0;
return x * x * x;
};
// Once you uncomment the type check in line 2,
// the cube() function should return 0.
cube("test");
To achieve that, we need to check the type of the argument that was passed in. If it wasn't a number, we probably shouldn't try to multiply it. We'll just return
0 in that case.var cube = function (x) {
if (typeof(x) !== 'number') return 0;
return x * x * x;
};
// Once you uncomment the type check in line 2,
// the cube() function should return 0.
cube("test");