简体   繁体   English

app.js:92未捕获的ReferenceError:未定义newCalculation

[英]app.js:92 Uncaught ReferenceError: newCalculation is not defined

Okay, I am noobie and currentlylearning javascript. 好的,我是noobie,目前正在学习JavaScript。 I was trying to create a web app by following some tutorial and decided to perform a task using an alternate method instead of just copying the code. 我试图通过遵循一些教程来创建Web应用程序,并决定使用替代方法来执行任务,而不仅仅是复制代码。

Now, I am stuck with the following 现在,我坚持以下几点

app.js:92 Uncaught ReferenceError: newCalculation is not defined app.js:92未捕获的ReferenceError:未定义newCalculation
at Object.calculateBudget (app.js:92) 在Object.calculateBudget(app.js:92)
at ctrlAddItem (app.js:221) 在ctrlAddItem(app.js:221)
at HTMLDocument. 在HTMLDocument。 (app.js:198) (app.js:198)

This is my code, I have highlighted the necessary lines 这是我的代码,我突出显示了必要的行

var budgetController = (function() {
  var Expense = function(id, description, value) {
    this.id = id,
      this.description = description,
      this.value = value
  }
  var Income = function(id, description, value) {
    this.id = id,
      this.description = description,
      this.value = value
  }
  var data = {
    allIteams: {
      exp: [],
      inc: []
    },
    totals: {
      exp: 0,
      inc: 0
    },
    budget: 0,
    percentage: 0
  };

  return {
    addItem: function(type, des, val) {
      var newItem, Id;

      if (data.allIteams[type].length > 0) {
        id = data.allIteams[type][data.allIteams[type].length - 1].id + 1;
      } else {
        id = 0;
      }


      if (type === 'exp') {
        newItem = new Expense(id, des, val);
      } else if (type === 'inc') {
        newItem = new Income(id, des, val);
      }
      data.allIteams[type].push(newItem);
      return newItem;
    },

    deleteItem: function(type, id) {
      var ditem;
      var ids = data.allIteams[type].map(function(current) {
        console.log(current.id);
        return current.id;
      });

      index = ids.indexOf(id);
      //ditem = data.allIteams.inc[index].value;
      console.log("value deleted is " + ditem);
      if (index !== -1) {
        data.allIteams[type].splice(index, 1); **
        newCalculation(); **
        /*
                    if (type === 'inc') {
                    data.budget = data.budget - ditem;
                        console.log("The income value to be subtracted " + data.budget);
                          document.querySelector('.budget__value').innerHTML = data.budget;


                    }
                      else if (type === 'exp') {
                        data.budget = data.budget + ditem;
                            console.log("The expense value to be subtracted is " + data.budget);
                                document.querySelector('.budget__value').innerHTML = data.budget;

                      }
        */
      }
    },

    calculateBudget: function(cur, type) {

      //calculate total income and expense
      if (type === 'inc') {
        data.totals[type] += cur.value;
        console.log(data.totals[type]);

      } else if (type === 'exp') {
        data.totals[type] += cur.value;
        console.log(data.totals[type]);
      } **
      newCalculation(); **
    },

    **
    newCalculation: function() {
      data.budget = data.totals.inc - data.totals.exp;
      if (data.totals.inc > 0) {
        data.percentage = Math.round((data.totals.exp / data.totals.inc) * 100);
      } **
    },


    getBudget: function() {
      return {
        budget: data.budget,
        percentage: data.percentage,
        totalInc: data.totals.inc,
        totalExp: data.totals.exp
      }
    },
    testing: function() {
      console.log(data);
    }


  };
})();

var UIController = (function() {

  var DOMstrings = {
    inputType: '.add__type',
    inputDescription: '.add__description',
    inputValue: '.add__value',
    addBtn: '.add__btn',
    incomeContainer: '.income__list',
    expenseContainer: '.expenses__list',
    container: '.container'
  };

  return {
    getInput: function() {
      return {
        type: document.querySelector(DOMstrings.inputType).value,
        description: document.querySelector(DOMstrings.inputDescription).value,
        value: parseFloat(document.querySelector(DOMstrings.inputValue).value)
      };
    },

    addListItems: function(obj, type) {
      var html, newHtml, element;

      //HTML String with Placeholder text
      if (type === 'inc') {
        element = DOMstrings.incomeContainer;
        html = '<div class="item clearfix" id="inc-%id%"><div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>';
      }
      if (type === 'exp') {
        element = DOMstrings.expenseContainer;
        html = '<div class="item clearfix" id="exp-%id%"><div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__percentage">21%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>';
      }
      //Replace Placeholder text with actual data
      newHtml = html.replace('%id%', obj.id);
      newHtml = newHtml.replace('%description%', obj.description);
      newHtml = newHtml.replace('%value%', obj.value);
      //Insert HTML with DOM
      document.querySelector(element).insertAdjacentHTML('beforeend', newHtml);

      //clears the input fields
      document.querySelector(DOMstrings.inputValue).value = "";
      document.querySelector(DOMstrings.inputDescription).value = ''
    },
    displayBudget: function(obj) {
      document.querySelector('.budget__value').innerHTML = obj.budget;
      document.querySelector('.budget__income--value').innerHTML = obj.totalInc;
      document.querySelector('.budget__expenses--value').innerHTML = obj.totalExp;
      document.querySelector('.budget__expenses--percentage').innerHTML = obj.percentage + "%";
    },

    deleteItemlist: function(selectID) {
      var el = document.getElementById(selectID);
      el.parentNode.removeChild(el);
    },

    getDOMStrings: function() {
      return DOMstrings;
    }
  }
})();





var Controller = (function(budgetCtrler, UICtrler) {
  //Event Listner called in the end
  var setUpEventListener = function() {
    var DOMs = UICtrler.getDOMStrings();
    document.querySelector(DOMs.addBtn).addEventListener('click', ctrlAddItem);
    document.querySelector(DOMs.container).addEventListener('click', ctrlDeleteItem);
    document.addEventListener('keypress', function(event) {
      if (event.keyCode === 13 || event.which === 13) {
        console.log("Enter is pressed");
        ctrlAddItem();
      }
    })
  }

  //All the exchange between Controllers
  var ctrlAddItem = function() {
    //Get the field input data

    var input = UICtrler.getInput();
    console.log(input);
    //creates an item to input

    if (input.description !== "" && !isNaN(input.value) && input.value > 0) {
      var newItem = budgetController.addItem(input.type, input.description, input.value);

      //displays the item in UI
      var listItem = UIController.addListItems(newItem, input.type);


      //calculate the budget and display the budget in UI

      //Calculate Budget
      budgetCtrler.calculateBudget(newItem, input.type);

      //return the budget
      var budget = budgetCtrler.getBudget();
      console.log(budget);
      //diplay the budget in UI
      UIController.displayBudget(budget);

    };

  }

  var ctrlDeleteItem = function(event) {
    var itemID, splitID, type, ID;

    itemID = (event.target.parentNode.parentNode.parentNode.parentNode.id);
    var input = UICtrler.getInput();
    var newItem = budgetController.addItem(input.type, input.description, input.value);

    if (itemID) {
      splitID = itemID.split('-');
      type = splitID[0];
      ID = parseInt(splitID[1]);
      console.log(type);
      console.log(id);

      //delete the ITEM from data structure
      budgetCtrler.deleteItem(type, ID);
      //delete the item from UI
      UICtrler.deleteItemlist(itemID);
      //displays the item in UI
    }

  }
  return {
    init: function() {
      console.log("The application have started working")
      setUpEventListener();
    }
  }
})(budgetController, UIController);

Controller.init();

Declare the newCalculation function outside the returned object, and assign that function to the returned objects property. 在返回的对象之外声明newCalculation函数,并将该函数分配给返回的对象属性。

Something like below: 如下所示:

var budgetController = (function() {
    ...
    var newCalculation = function() {
      data.budget = data.totals.inc - data.totals.exp;
      if (data.totals.inc > 0) {
        data.percentage = Math.round((data.totals.exp / data.totals.inc) * 100);
      } **
    };

    ...

    return {
        ...
        newCalculation: newCalculation,
        ...
    }
})();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的ReferenceError:app.js中未定义require:3 - Uncaught ReferenceError: require is not defined at app.js:3 未捕获的ReferenceError:未定义角度app.js - Uncaught ReferenceError: angular is not defined app.js AngularJS:未捕获ReferenceError:未定义angular app.js:1(匿名函数): - AngularJS: Uncaught ReferenceError: angular is not defined app.js:1(anonymous function): app.js:63 Uncaught ReferenceError: GLTFLoader is not defined - app.js:63 Uncaught ReferenceError: GLTFLoader is not defined Laravel Uncaught ReferenceError webpackJsonp未在app.js:1中定义 - Laravel Uncaught ReferenceError webpackJsonp is not defined at app.js:1 将使用 CDN 链接创建的 React App 导入 App.js 时出现“未捕获的 ReferenceError:require is not defined” - “Uncaught ReferenceError: require is not defined” when importing into App.js for React App created using CDN links Uncaught ReferenceError: Glide is not defined - 尽管 Glide 像所有其他的一样在 app.js 中加载 - Uncaught ReferenceError: Glide is not defined - Although Glide loaded in app.js like all others 在 Laravel 中将 JS 添加到 app.js 时出现未捕获的 ReferenceError - Uncaught ReferenceError when adding JS to app.js in Laravel app.js:3524 Uncaught ReferenceError: THREE not defined at Object../node_modules/three-bmfont-text/index.js - app.js:3524 Uncaught ReferenceError: THREE is not defined at Object../node_modules/three-bmfont-text/index.js JS -> Uncaught ReferenceError: Dolphins 未定义 - JS -> Uncaught ReferenceError: Dolphins is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM