I'm a beginner in JS and just trying to understand what is going on in below code. Probably it's something stupid but - well, like I sayed, Im a beginner ;)
First it just split array which is an argument of makeFriendlyDates(['2016-07-01', '2016-08-04']);
function.
So, at this stage everything is looking ok. However when I call function changeToFriendly(arr1);
which only reason to exist is changing numbers into month name it somehow changes newArr
as well.
So if I comment out this line: //changeToFriendly(newArr);
then newArr
is what is should be, but if the changeToFriendly(newArr);
is called out it somehow changes newArr
instead of just returning month name.
My question is how changeToFriendly(newArr);
can change newArr
if the function doesn't do anything with the array, just traversing it and checking number corresponding with month name.
function makeFriendlyDates(arr) {
var newArr = [];
var elem;
for (elem in arr) {
newArr.push(arr[elem].split('-'));
}
document.getElementById('result').innerHTML = newArr;
function changeToFriendly(arr1) {
var month = '';
var elem1;
for (elem1 in arr1) {
if (arr1[elem1][1] = '01') {
month = 'January';
} else if (arr1[elem1][1] === '02') {
month = 'February';
} else if (arr1[elem1][1] === '03') {
month = 'March';
} else if (arr1[elem1][1] === '04') {
month = 'April';
} else if (arr1[elem1][1] === '05') {
month = 'May';
} else if (arr1[elem1][1] === '06') {
month = 'June';
} else if (arr1[elem1][1] === '07') {
month = 'July';
} else if (arr1[elem1][1] === '08') {
month = 'August';
} else if (arr1[elem1][1] === '09') {
month = 'September';
} else if (arr1[elem1][1] === '10') {
month = 'October';
} else if (arr1[elem1][1] === '11') {
month = 'November';
} else if (arr1[elem1][1] === '12') {
month = 'December';
}
}
document.getElementById('result1').innerHTML = newArr;
return month;
}
changeToFriendly(newArr);
}
makeFriendlyDates(['2016-07-01', '2016-08-04']);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Title</title>
</head>
<body>
<p id="result"></p>
<p id="result1"></p>
</body>
</html>