简体   繁体   中英

Function works incorrectly when called by another function

First post here. I am trying to create an Easter Calculator for a school project. The function pasCalc(E) works great when called on its on the browser console. 效果很好。 However, when I type a year in my html , all the dates are off. 键入年份时,所有日期均已关闭。

Sorry if this sounds like a newb question, but I can't figure out what's wrong... Any help will be greatly appreciated, thank you for your time! :)

 function start() { document.getElementById("orthodox").innerHTML = "Ορθόδοξο πάσχα:"; //var E = document.getElementById("inputfield").value; //console.log(E); var pasxa = pasCalc(document.getElementById("inputfield").value) console.log(pasxa); //console.log(pasxa[0] + " " + pasxa[1]); document.getElementById("orthodox").innerHTML += " " + pasxa; } function pasCalc(E) { //var E = 2018; var a = E % 19; var T = (8 + 11 * a) % 30; var month = "Mach"; var K = Math.floor((E / 100) - (E / 400) - 2); var iPanArx = 21 + (53 - T) % 30; if (iPanArx > 31) { var iPanArxM = iPanArx - 31; month = "April"; } else { var iPanArxM = iPanArx; } var iPanTel = iPanArxM + K; var Y = (E + Math.floor(E / 4) + iPanArx) % 7; var iPas = iPanTel + (7 - Y); if (iPas > 30 && month == "April") { month = "May"; var iPas = iPas - 30; } else if (iPas > 31 && month == "Marchυ") { month = "April"; var iPas = iPas - 31; } console.log(iPas + " " + month + " " + E); var arr = []; arr.push(iPas); arr.push(month); return arr; } 
 <div class="container align-center"> <h1>Easter Calculator</h1> <input id="inputfield" type="text" placeholder="Enter a Year..."> <button class="btn btn-primary" onclick="start();">Submit</button> <h5 id="orthodox">Orthodox Easter:</h5> <h5>Catholic Easter:</h5> </div> 

Form fields, by default, are strings. In order for Javascript to know it's an integer so that it works with numeric calculations like the year component of a date, wrap it in a call to parseInt()

var pasxa = pasCalc(parseInt(document.getElementById("inputfield").value))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM