0

I'm very new to programming and have some problems. I'would like to create one form (in html and javascript), where I can do some calculations. This is about numbers and years. So at first I need to put input field, where I can type year (like 2013). This input value will be connected to several functions:

  1. calculating difference from current year (if I ask for 2018 it should write 5, or if I ask for 2000 iz shoul write -13 and so on...)
  2. check if the year is leap year (true/false or yes/no, ...)
  3. calculate the sum of the numbers i ask (2018 = 11, 2013=6, ...)
  4. if the number is Prime number (true/false, yes/no, ...)
  5. reverse number (2013 = 3102, 2018=8102, ...)
  6. show the chinese zodiac sign for year
  7. convert year to roman numerals (1=I, 2=II, 10=X, ...)

I did find some function, but i just can't put this together that will work. I would be very helpful if someone can help with this.

Examples i find on internet:

function isleap() {
    var yr = document.getElementById("year").value;
    if ((parseInt(yr) % 4) == 0) {
        if (parseInt(yr) % 100 == 0) {
            if (parseInt(yr) % 400 != 0) {
                alert("Not Leap");
                return "false";
            }
            if (parseInt(yr) % 400 == 0) {
                alert("Leap");
                return "true";
            }
        }
        if (parseInt(yr) % 100 != 0) {
            alert("Leap");
            return "true";
        }
    }
    if ((parseInt(yr) % 4) != 0) {
        alert("Not Leap");
        return "false";
    }
}

reverse:

<script type="text/javascript">
    var year, b = 0;
    year= parseInt(prompt("Enter year: "));

    document.write("You've entered: " + year+ "<br />");
    while(year> 0) {        
        b = b * 10
        b = b + parseInt(year% 10)
        year= parseInt(year/ 10)
    }
    document.write("Reverse numbers: ", b);

</script>

sum numbers:

<script>
function find() {
    var sum = 0;
    var no = parseInt(frm.txt1.value);
    while (no > 0) {
        sum = sum + no % 10;
        no = Math.floor(no / 10);
    }
    alert("Sum of digits  " + sum);
}
</script>

<form name="frm">
    Enter a Number:<input name="txt1" type="text" />
    <input name="b1" onClick="find();" type="button" value="display" />
</form>

5 Answers 5

2

It is considered a best practice to separate concerns, I use jQuery as my DOM abstraction library - it's really easy.

With an input like this:

<input id="b1" name="b1" type="button" value="display" />

You could use the jQuery on method like this:

$('#b1').on('click', function (event) { alert('I was clicked'); });

$('#b1').on('click', function (event) { alert('I heard that click too'); });

You can call any function:

$('#b1').on('click', isLeap);

Just don't forget to include jQuery on the page with this snippet:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

1

I would define functions independent of form with input parameter. Then you just pass that parameter to each function and display the result as you wish.

<script type="text/javascript">
function isLeap(yr) {
    if ((parseInt(yr) % 4) == 0) {
        if (parseInt(yr) % 100 == 0) {
            if (parseInt(yr) % 400 != 0) {
                return "false";
            }
            if (parseInt(yr) % 400 == 0) {
                return "true";
            }
        }
        if (parseInt(yr) % 100 != 0) {
            return "true";
        }
    }
    if ((parseInt(yr) % 4) != 0) {
        return "false";
    }
}

function reverse(year) {
    var b = 0;
    year = parseInt(year);
    while (year > 0) {
        b = b * 10
        b = b + parseInt(year % 10)
        year = parseInt(year / 10)
    }
    return b;
}

function sum(no) {
    var sum = 0;
    while (no > 0) {
        sum = sum + no % 10;
        no = Math.floor(no / 10);
    }
    return sum;
}

function outputData() {
    var year = form.year.value;
    alert("Is leap: " + isLeap(year));
    alert("Reversed: " + reverse(year));
    alert("Sum: " + sum(year));
}
</script>

<form name="form">
    Enter a Number:<input name="year" type="text" />
    <input name="b1" onClick="outputData();" type="button" value="display" />
</form>

Comments

1
   <input name="b1" onClick="function1(),function2()" type="button" value="display" />

     you can call like this

   or
  <input name="b1" onClick="callAllFunctions();" type="button" value="display" />

function callAllFunctions(){
  function1();
  function2();
}

1 Comment

This looks fine. Will try it now. Thanks a lot. Perhaps I will ask for another help if I need it :)
1

You can just create one function that calls each of the other functions and then set that as the onclick function for the form.

function processData(){
    function1();
    function2();
    function3();

}

<input name="b1" onClick="processData();" type="button" value="display" />

Seperating the different function calls with a comma like PSR suggested also works, but I think this result is nicer because it keeps the markup relatively clean, and lets you change the logic in the script as needed without having to directly edit the markup or keep adding functions as needed. You can also pass the results of one function to another directly if needed this way.

Comments

1

Something like this. This is only example...

<input type="text" name="year" id="year" value="" />
<input type="button" name="process" value="processYear" id="proc" onclick="process()"/>


<script>
function isLeap(val)
{   
    if (val % 4 ==0 && ((val % 100 ==0 && val % 400 ==0) || (val % 100 !=0)))
    {
        console.log("Leap...");
        return true;
    }
    else
    {
        console.log("Not Leep...");
        return false;
    }
}

function _revers(val)
{
    _val = val+"";
    return _val.split("").reverse().join("");
}

function sum(val)
{
    _val = val+"";
    return _val.split("").reduce(function(previousValue, currentValue){
                return currentValue + parseInt(previousValue);
            })
}

function diff(val)
{
    return (new Date()).getFullYear() - val;
}

function process()
{
    val = parseInt(document.getElementById("year").value)|0;
    isLeap(val);
    console.log('reverse:', _revers(val));
    console.log('sum:', sum(val));
    console.log('diff:', diff(val));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.