Currently, I am attempting to showcase my JavaScript array in an ordered list format, like you would typically see in an HTML element. For example, if the array of strings passed is: pets(['cat', 'dog', 'mouse'])
, I aim to display:
1: cat
2: dog
3: mouse
However, I have a specific method in mind for achieving this. Please refer to the following code snippet:
function logArrayElements(element, index, array) {
return index + ':' + element;
}
var pets = function(array){
if (array.length <= 0) {
return [];
} else {
return array.forEach(logArrayElements);
}
}
Upon executing the above code, I end up with 'undefined'. What could be causing this issue?
Just to clarify, this is not related to any academic assignment but rather part of my self-learning journey. You can also view the demonstration on fiddle: http://jsfiddle.net/emporio/ohfrhfs5/14/