Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6217033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:21:18+00:00 2026-05-24T07:21:18+00:00

I’ve been reading O’Reilly’s JavaScript: The Definitive Guide (6th ed), and have been examining

  • 0

I’ve been reading O’Reilly’s JavaScript: The Definitive Guide (6th ed), and have been examining the very first example program — a loan calculator. However, the structure of this just bugs me — I know that it’s a book example, and hence probably not the best way of going about things for the sake of simplicity, so I’m curious as to how a professional using best practices would structure the same thing.

For example, the main function, calculate(), which is called onChange to any of the input fields, contains within it a TON of stuff that doesn’t have anything to do with calculating anything — display code, for instance — how would you pull that out to separate concerns?

It’d make me feel better if you could modularize the individual tasks that are to be done and put the major control into the function something like this:

run() {
    getdata();
    calculate();
    if (isFinite(monthly)) {
        display();
        save(arg1, arg2, arg3, arg4); }
    else {
        cleardisplay(); }

But I have no idea what I’m talking about. Is there some way to do it with the Object Literal pattern? I guess I’m just asking how you would do it idiomatically.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:21:18+00:00Added an answer on May 24, 2026 at 7:21 am

    This is a very contrived example, but you can see some concepts here on how to separate “business logic” from both the input data and the display output. I’ve used jQuery here just for the $, .val(), and .click() helper functions, you could use any other library, or none at all.

    HTML:

    <p>First Number</p>
    <p><input type="text" id="num1"></p>
    <p>Second Number</p>
    <p><input type="text" id="num2"></p>
    <p>Third Number</p>
    <p><input type="text" id="num3"></p>
    <p><input type="button" value="Calculate" id="btntrigger"></p>
    <p>Result</p>
    <p><input type="text" id="result"></p>
    

    Javascript:

    function Calculator(options) {
        this.options = options;
    }
    
    Calculator.prototype.run = function() {
        return (parseInt(this.options.getN1()) + parseInt(this.options.getN2())) *this.options.getN3();
    };
    
    var calc = new Calculator({
        getN1: function() { return $('#num1').val(); },
        getN2: function() { return $('#num2').val(); },
        getN3: function() { return $('#num3').val(); },
    });
    
    
    $('#btntrigger').click(function(){
        $('#result').val(calc.run());
    });    
    

    Live Demo: http://jsfiddle.net/Wd49U/

    Some notes:

    The input is handled by passing in callback functions which return the value to be used, this callback style offers extreme flexibility in where and how you want to get your data into the calculator.

    I assign the “click” handler outside of the calculator function because that method of triggering it, isn’t really the calculators responsibility, maybe in another case you would want to call run() on a timer, or from another event.

    Most importantly, it really depends on your specific use case for what the best way might be. I hope this helps you get some ideas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.