Javascript Fix

2

I have this script, it works, this program caputura the information of the months of the year, and shows them, is within a for cycle, the program that I have is the following:

I want you to print the month's message like this:

The first month is: The second month is: The third month is:

..... month is:

That is to say that these words (first, second, third) is a variable or an accumulator, how could you do it for this?

<script type="text/javascript">
 function Mostrarmeses(meses) {
 	var meses = ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"]; 
 	 	 	
 	 document.write('<br>'); 

 	 for (var i = 0; i<12; i++) 
    {
 
    		 document.write('<br>'); 
      document.write("El mes es "+meses[i]);
      	 document.write('<br>'); 
    }
    
 }



</script>
<html>
<head>
<title>Meses del año</title>
<meta charset="utf-8">
<style type="text/css">
	body	{
background-color:#FF0000; 
	}
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<font color="#030303" face="georgia" size="5"> 
<center>
<TABLE BORDER=3 width="1000" height="50">
<TR><TD>
<font face="georgia" size="30">
<MARQUEE SCROLLAMOUNT=10 BEHAVIOR="alternate"><b>Meses del año</b></MARQUEE>
</font>
</TD></TR>
</TABLE>

<button type='button' onclick='Mostrarmeses()'>Presiona para ver los meses</button><br/>

</center>
</font>
</body>
</html>
    
asked by Graffters Skates 16.10.2017 в 21:38
source

2 answers

1

One possible solution: just like you have the months, with a fix :

<script type="text/javascript">
 function Mostrarmeses(meses) {
    var meses = ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"];
    var order = [ 'primer', 'segundo', 'tercero', 'cuarto', 'quinto', 'sexto', 'septimo', 'octavo', 'noveno', 'decimo', 'decimoprimero', 'decimosegundo' ];

     document.write('<br>'); 

     for (var i = 0; i<12; i++) 
    {

             document.write('<br>'); 
      document.write("El " + order[i] +" mes es "+meses[i]);
         document.write('<br>'); 
    }

 }

</script>
    
answered by 16.10.2017 в 21:44
0

Here it is, the code will appear in the same document and I also use DOMContentLoaded , to make sure that the document has loaded in its entirety.

window.addEventListener("DOMContentLoaded",()=>{
document.getElementById("ver").addEventListener("click",m);
});
function m(){
  var meseslistos = document.getElementById("meseslistos");
  var meses = [
    "Enero",
    "Febrero",
    "Marzo",
    "Abril",
    "Mayo",
    "Junio",
    "Julio",
    "Agosto",
    "Septiembre",
    "Octubre",
    "Noviembre",
    "Diciembre"
  ],max = meses.length,i = 0,im = document.getElementById("meseslistos");
  var t = [ 'primer', 'segundo', 'tercero', 'cuarto', 'quinto', 'sexto', 'septimo', 'octavo', 'noveno', 'decimo', 'decimoprimero', 'decimosegundo' ];

  for(;i<max;i++){
   im.innerHTML += '<hr><b>El ${t[i]} mes, es: ${meses[i]}</b>';
  }
  
}
<html>
<head>
<title>Meses del año</title>
<meta charset="utf-8">
<style type="text/css">
	body	{
background-color:#FF0000; 
	}
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<font color="#030303" face="georgia" size="5"> 
<center>
<TABLE BORDER=3 width="1000" height="50">
<TR><TD>
<font face="georgia" size="30">
<MARQUEE SCROLLAMOUNT=10 BEHAVIOR="alternate"><b>Meses del año</b></MARQUEE>
</font>
</TD></TR>
</TABLE>

<button type='button' id="ver">Presiona para ver los meses</button><br/>
  <i id="meseslistos"></i>
</center>
</font>
</body>
</html>
    
answered by 17.10.2017 в 18:42