I am a javascript beginner, and here is the 5 coding problems that I am tackling with.
1.Declare a variable called myName and assign to an array populated with two strings: 1) your first name and 2) your last name.
2.Declare a function called join that can take two inputs: 1) an array and 2) a separator string. The separator string is optional.
3.join concatenates all the elements of the input array with the input string and returns the result. The output of join is always a string.
4.If no separator is provided, the default separator should be a string space ' '. If no input array is provided or the array is empty, join should return an empty string ''.
5.You cannot use the built-in join method for arrays and we highly recommend avoiding other built-in methods.
// I have written some codes below so far, and it seems I've completed 1~3. However, I have no clue to solve 4 and 5. How can I solve them?
// Error message said: The join function should default to using a space when a separator is not provided and should return an empty string if no array is provided
var myName = ["FirstName", "LastName"];
function join(arr,sep) {
result = arr.join(sep);
return result;
};
${sep}${arr[i]}; } return result; }; ` Ps: The homework question is not welcome in stackoverflow.