Solution Review: Validate Arguments
This lesson will explain the solution to the problem in the previous lesson.
We'll cover the following...
Solution #
Press + to interact
var concat = function(string1, string2) {if (typeof string1 === 'undefined' && typeof string2 === 'undefined') {throw new TypeError('Both arguments are not defined')}else if(typeof string1 === 'undefined'){throw new TypeError('First argument is not defined');}else if(typeof string2 === 'undefined') {throw new TypeError('Second argument is not defined');}if(typeof string1 != "string" || typeof string2 != "string"){throw new TypeError('not string')}return string1 + string2;}console.log(concat("a","b"))try{console.log(concat("a"))}catch(e){console.log(e)}try{console.log(concat(1,2))}catch(e){console.log(e)}try{console.log(concat(undefined,"bd"))}catch(e){console.log(e)}try{console.log(concat())}catch(e){console.log(e)}
Explanation #
We need to validate the following cases in our function:
-
is
string1
defined? -
is
string2
...