Write
a program, that takes an element and return an array.
a. Element can be of any
type like number, boolean, string, object etc.,
b. If the element is
null (or) undefined, you should return an empty array.
c. If element is an
array itself, you should return ele only.
HelloWorld.js
function get_array(ele){ if(ele === null || ele === undefined){ return []; } if(Array.isArray(ele)) return ele; return [ele]; } console.log(get_array(null)); console.log(get_array(undefined)); console.log(get_array([])); console.log(get_array([123, 456])); console.log(get_array(123)); console.log(get_array("Krishna"));
Output
Array
[]
Array
[]
Array
[]
Array
[ 123, 456 ]
Array
[ 123 ]
Array
[ "Krishna" ]
No comments:
Post a Comment