locked
Basic Knockout Function RRS feed

  • Question

  • User1103919330 posted

    Could someone please advice me how can calculte the total number of hours (totalHours) from the function shown below. 

        <script type="text/javascript">
    
    
            function timeSheetEntry(name, hours) {
    
                this.name = name;
                this.hours = ko.observable(hours);
                this.wages = ko.computed(function () {
                   return this.hours()*25;
    
                }, this);
    
                
            }
    
               
            function ViewModel() {
    
                this.days =
                    [
                        { dayName: "Monday" },
                         { dayName: "Tuesday" },
                         { dayName: "Wednesday" },
                         { dayName: "Thursday" },
                         { dayName: "Friday" },
                         { dayName: "Saturday" },
                         { dayName: "Sunday" }
                        
                    ];
                this.timeSheetEntries = ko.observableArray([
                        new timeSheetEntry(this.days[0], "", ""),
                        new timeSheetEntry(this.days[1], "","")
                    ]);
    
                this.addEntry = function() {
    
                    this.timeSheetEntries.push(new timeSheetEntry(this.days[0], "", ""));
                };
    
                this.totalHours = ko.computed(function () {
                    var total = 0;
                    for (var i = 0; i < this.timeSheetEntries().length; i++)
                        total += this.timeSheetEntries.hours;
                    return total;
                }, this);
                
                this.removeEntry = function (entries) {
                    this.timeSheetEntries.remove(entries);
                }.bind(this);
                
            }
    
            ko.applyBindings(new ViewModel());
    
        </script>



    Tuesday, June 12, 2012 7:39 PM

Answers

  • User1779161005 posted

    Umm, without running this, it's a bit hard to tell, but it looks as if this line:

    total += this.timeSheetEntries.hours;

    needs to be this instead:

    total += this.timeSheetEntries()[i].hours;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 12, 2012 9:04 PM

All replies

  • User1779161005 posted

    Umm, without running this, it's a bit hard to tell, but it looks as if this line:

    total += this.timeSheetEntries.hours;

    needs to be this instead:

    total += this.timeSheetEntries()[i].hours;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 12, 2012 9:04 PM
  • User581921910 posted

    try this .

     this.totalHours = ko.computed(function () { 
                   
    var total = 0; 
                   
    for (var i = 0; i < this.timeSheetEntries().length-1; i++) 
                        total
    += this.timeSheetEntries[i].hours; 
                   
    return total; 
               
    }, this); 


    are you getting any error ?

    Wednesday, June 27, 2012 1:56 AM