I need to implement the solution function such as running the following line:
JavaScript
x
2
1
console.log(solution('Hello You !'))
2
gives the following output (one word per line):
Hello
You
!
The input parameter is always a non-null character string. So i did that code:
JavaScript
1
10
10
1
function solution(input){
2
3
arr=input.split(" ");
4
for(var i=0;i<arr.length;i++) {
5
console.log(arr[i]);
6
}
7
8
}
9
input="Hello You !";
10
console.log(solution('Hello You !'));
But when i run it , i get the result:
Hello
You
!
undefined
Why the result of my code snippet displays “undefined“?
What is that “undefined“? how can i fix it?
Advertisement
Answer
You’re not returning anything, you have to return the arr
JavaScript
1
9
1
function solution(input){
2
3
arr=input.split(" ");
4
for(var i=0;i<arr.length;i++) {
5
console.log(arr[i]);
6
}
7
8
return arr;
9
}
When you call a function, the value will be its return value. If no return value is specified, a function returns undefined.