How to get months return with While loop in Javascript -
I am learning JS and I have trouble finding this. I want to ask that one of you can discover my error. I want to use a loop to return the names of all the months of January without using an array, is it possible? Thank you.
window.onload = function () {document.getElementById ("months"). InnerHTML = getMonth (11); }; For (var month = 0; month <11; month ++) function getMonth (month) {var monthName; If (months == 11) {monthname = "December"; } Return month name; } Function getMonth () {var x = "", months = 0; While (month <11) {x = x + month + "
"; I ++; } Document.getElementById ("months"). InnerHTML = x; }
without an array: Function Mill (Months) {Switch (Month) {Case: Return "Jan"; Case 1: Return "February"; // ... case 11: return "December"; Default: return "Not valid months"; }} But there is a painful way of doing this. With the array:
function getMonth (month) {var monthNames = ["January", "February", "March", "April", "May" , "June", "July", "August", "September", "October", "November", "Dec"]; Return month name [month]; } I do not know what you want to do with a loop for a while without any array. What will happen to you again?
Comments
Post a Comment