1

I am creating a budget app with HTML,CSS and Javascript considering the module structure,closures and Immediately Invoked (IIFE) principles. I have divide module structure into UIController ,Appcontroller,BudgetController.

I have a getinput method in UI Controller which returns an object with the input values from front end and i am calling this getinput from app controller.But i am getting error under line getinput method .Is there any syantax error or i am doing something wrong.

enter image description here

enter image description here

Script.js

var budgetController=(function(){
    //some code
})();

/*--------------------------------------------------------------*/

// UI Controller
var UIController=(function(){
    return
    {
        getInput:function(){
            return {
                type: document.querySelector('.add__type').value,
                description: document.querySelector('.add__description').value,
                value: document.querySelector('.add__value').value
            };
        }
    };
})();

/*--------------------------------------------------------------*/

// App controller
var controller=(function(budgetctrl,UIctrl)
{
    var ctrlAddItem=function()
    {
        var result=UIctrl.getInput();
        console.log(result);
        // get the input data

        // add the budget item to budget controller

        // add the item to ui controller

        // calculate the budget

        // display the result 
    }

    // Button click event

    document.querySelector('.add__btn').addEventListener('click',ctrlAddItem);

    //event key event

    document.addEventListener('keypress',function(event)
    {
        if(event.keyCode===13 || event.which ===13)
        {
            ctrlAddItem();
        }
    });
})(budgetController, UIController)
3
  • You have the error message and the line it originates? Why stops you from fixing it? Commented Dec 20, 2017 at 5:45
  • Actually i am learning advanced javascript from tutorials and they have implemented the same thing and the syantax is also same ,i am getting this error in browser console .Just to ensure is is there any syntax error in the line or i am doing something wrong Commented Dec 20, 2017 at 5:49
  • see the error ,can you help me : drive.google.com/open?id=1COs1WxHD5ZABsc9ka9mZD7G9yeVFDS8U Commented Dec 20, 2017 at 5:51

1 Answer 1

2

The problem is here:

return
{

If you move the { up to the same line as the return the syntax error will go away:

return {

The error happens because of automatic semicolon insertion. JavaScript puts a semicolon after the return for you, so the code is effectively:

return;
{

which is definitely not what you wanted.

When you are writing JavaScript code and have to put a { somewhere, always put it on the same line as the statement before, not on a line by itself. I believe this specific problem happens only with return statements, not other statements like if for example, but it's good to be consistent.

For example you don't get the error on these lines:

var controller=(function(budgetctrl,UIctrl)
{
    var ctrlAddItem=function()
    {

But since you have to follow the "brace on same line" style for the return, it's more consistent to do it everywhere (and add a bit more whitespace to improve readability while you're at it):

var controller = (function( budgetctrl, UIctrl ) {
    var ctrlAddItem = function() {
Sign up to request clarification or add additional context in comments.

1 Comment

superrrrr !!!! thnku ,it works ,did not read about return thing in javascript,will keep in mind

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.