Challenge: Validate Arguments

This challenge will test your skills in implementing "typeof" operator in JavaScript.

Problem statement #

The concat function takes two strings and adds them together. Your task includes implementing this functionality and validating the function arguments as well. You need to check for the following cases:

  • Case 1: is string1 defined?

  • Case 2: is string2 defined?

  • Case 3: are both string1 and string2 defined?

  • Case 4: are both string1 and string2 strings?

You need to validate these cases and only then concatenate the arguments passed. If any of the following cases are false, you need to throw the following errors:

  • Case 1: First argument is not defined

  • Case 2: Second argument is not defined

  • Case 3: Both arguments are not defined

  • Case 4: not string

You will require the use of the TypeError object.

Input #

Two strings

Output #

Concatenated Strings

Sample input #

concat("a","b")
concat("a")
concat(1,"c")
conact(undefined,"x")
concat()

Sample output #

"ab"
TypeError: "Second argument is not defined"
TypeError: "not string"
TypeError: "First argument is not defined"
TypeError: "Both arguments are not defined"

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.