Dimitris Zarachanis
Dimitris Zarachanis

Reputation: 11

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 own on the browser console. However, when I type a year in my html form, 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>

Upvotes: 0

Views: 61

Answers (1)

Lee Benson
Lee Benson

Reputation: 11599

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))

Upvotes: 1

Related Questions