Math Problem in TS

Hi!
My Problem is simple. I want to add two numbers. But it’s not working. I know that’s it’s a common problem, but I can’t find a solution. I’m missing something.

I search and tried everything I could find which includes the following:

  • adding + in front
  • using Number()
  • even something like Number.parseInt(var.toString())

But nothing is working. Now some more details.

  • Ionic 3.8.1

My template

<ion-header>
  <ion-navbar>
    <ion-title>Pyramide</ion-title>
    <ion-buttons end>
      <button ion-button icon-only (click)="openHelp()">
        <ion-icon name="help"></ion-icon>
      </button>
    </ion-buttons>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <ion-list>
    <ion-item>
      <ion-label>Modus Hoch</ion-label>
      <ion-toggle checked="true" [(ngModel)]="up"></ion-toggle>
    </ion-item>
    <ion-item>
      <ion-label>Belastung</ion-label>
      <ion-datetime displayFormat="mm:ss" (ionChange)="calculate()" [(ngModel)]="load"></ion-datetime>
    </ion-item>
    <ion-item>
      <ion-label>Delta</ion-label>
      <ion-datetime displayFormat="mm:ss" [(ngModel)]="delta"></ion-datetime>
    </ion-item>
    <ion-item>
      <ion-label>Pause</ion-label>
      <ion-datetime displayFormat="mm:ss" [(ngModel)]="pause"></ion-datetime>
    </ion-item>
  </ion-list>

  <hr>

  <ion-list no-lines>
    <ion-item disabled>
      <ion-label color="grey">Runden</ion-label>
      <ion-label text-right color="grey">{{ total_rounds }}</ion-label>
    </ion-item>
    <ion-item>
      <ion-label color="grey">Belastung Gesamt</ion-label>
      <ion-label text-right color="grey">{{ total_load }}</ion-label>
    </ion-item>
    <ion-item>
      <ion-label color="grey">Pyramide Gesamt</ion-label>
      <ion-label text-right color="grey">{{ total_pyramide }}</ion-label>
    </ion-item>
  </ion-list>  

  <button ion-button large block>Go!</button>

</ion-content>

TS File

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ModalController } from 'ionic-angular';

import * as moment from 'moment';

import { PyramideHelpPage } from '../pyramide-help/pyramide-help';

@IonicPage()
@Component({
  selector: 'page-pyramide',
  templateUrl: 'pyramide.html',
})
export class PyramidePage {

  up = true;
  load = '00:01:00';
  delta = '00:00:30';
  pause = '00:01:00';

  total_rounds:number = 0;
  total_load  = '0';
  total_pyramide ='00';

   
  

  constructor(public navCtrl: NavController, public navParams: NavParams, public modalCtrl: ModalController) {}

  
  calculate() {
    let rounds:number = 0;
    let diff:number;
    let total_load;
    let total_pyramide:number;

    let loadSec = moment.duration(this.load).asSeconds();
    let deltaSec = moment.duration(this.delta).asSeconds();
    let pauseSec = moment.duration(this.pause).asSeconds();
    console.log("Load sec:"+loadSec);
    console.log("Delta sec:"+deltaSec);
    diff = loadSec;
    total_load = diff;
    do { 
      
      diff = diff - deltaSec; 
      rounds++; 

      total_load = total_load + diff;
      console.log(rounds);
      console.log(diff);
      console.log(total_load);
   } while(diff>deltaSec); 

   this.total_rounds = rounds;

   total_load = moment.duration(total_load, 's');
   this.total_load = moment(total_load.minutes(), "m").format("mm") + ":" + moment(total_load.seconds(), "s").format("ss");

   total_pyramide = Number.parseInt(pauseSec.toString()) * Number.parseInt(rounds.toString()) + Number.parseInt(total_load.toString());
   console.log(total_pyramide);
   //String(total_pyramide) = moment.duration(String(total_pyramide), 's');
   //this.total_pyramide = moment(total_pyramide.minutes(), "m").format("mm") + ":" + moment(total_pyramide.seconds(), "s").format("ss");

  

  }
  openHelp() {
    let helpModal = this.modalCtrl.create(PyramideHelpPage);
    helpModal.present();
  }

  ionViewDidLoad() {
    //console.log('ionViewDidLoad PyramidePage');
  }

}

The line total_pyramide = Number.parseInt(pauseSec.toString()) * Number.parseInt(rounds.toString()) + Number.parseInt(total_load.toString()); gives me headaches!

It’s as simple as 60*9 + 1650 but instead of getting 2190 I get 1650540.
He isn’t adding the numbers…

You’re adding two strings together, that’s what it does. Do a .toString() only to the total amount after calculations.

Oh man, I found the mistake.
total_load was in ms and not s as I thought.
So that’s the funny part. Adding the number would give you 1650540 since total_load = 1650000. Just divide it by a thousand and I’m good to go.

Ok, that was funny. Back to work.:rofl: