typescript error caused by NaN

Apologies for the repetitive question, but I am really struggling to find a solution. I am facing an issue with this calculation. The parameters a to g represent the values of my input from the HTML. I need to use these values to calculate a sum. When I try console.log(+this.a), it gives me a number. However, console.logging(this.aa) results in NaN. Can someone please guide me on how to address this problem? Thank you.

Here is a snippet from my TypeScript file:

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


@IonicPage()
@Component({
  selector: 'page-song',
  templateUrl: 'song.html',
})
export class SongPage {
  a:string;aa:number = +this.a * 2;
  b:string;bb:number = +this.b * 7;
  c:string;cc:number = +this.c * 6;
  d:string;dd:number = +this.d * 5;
  e:string;ee:number = +this.e * 4;
  f:string;ff:number = +this.f * 3;
  g:string;gg:number = +this.g * 2;
  sumA:number = (this.aa+this.bb+this.cc+this.dd+this.ee+this.ff);
  sumB:number = this.sumA%11;
  sumC:number = 11-this.sumB;

  char: string;
  constructor(public navCtrl: NavController, public navParams: NavParams) {
  }
  cal()
  {
    console.log(this.aa);
    if(this.sumC == 1)
    {
      this.char = 'A';
    }
    else if(this.sumC == 2)
    {
      this.char = 'B';
    }
    else if(this.sumC == 3)
    {
      this.char = 'C';
    }else if(this.sumC == 4)
    {
      this.char = 'D';
    }else if(this.sumC == 5)
    {
      this.char = 'E';
    }else if(this.sumC == 6)
    {
      this.char = 'F';
    }else if(this.sumC == 7)
    {
      this.char = 'G';
    }else if(this.sumC == 8)
    {
      this.char = 'H';
    }
    else if(this.sumC == 9)
    {
      this.char = 'I';
    }else if(this.sumC == 10)
    {
      this.char = 'Z';
    }else if(this.sumC == 11 || 0)
    {
      this.char = 'J';
    }else
    {
      this.char = 'NaN';
    }
  }

}

And here is the relevant part from my HTML file:

  <ion-input name="two" class="box" maxlength="1" [(ngModel)]="a"></ion-input>
  <ion-input name="three" class="box" maxlength="1" [(ngModel)]="b"></ion-input>
  <ion-input name="four" class="box" maxlength="1" [(ngModel)]="c"></ion-input>
  <ion-input name="five" class="box" maxlength="1" [(ngModel)]="d"></ion-input>
  <ion-input name="six" class="box" maxlength="1" [(ngModel)]="e"></ion-input>
  <ion-input name="seven" class="box" maxlength="1" [(ngModel)]="f"></ion-input>
  <ion-input name="eight" class="box" maxlength="1" [(ngModel)]="g"></ion-input>
  <button ion-button round block (click)="cal()">Validate</button>
  <h2>The character is: {{char}}</h2>

Answer №1

x to y are not initialized, so it is necessary to assign values to those variables before performing calculations for xx or yy ... :

 initializeVariables(){
   this.x = // <-- add value 
   this.y = // <-- add value 
   //...
   this.z = // <-- add value 

  this.xx = +this.x * 2;
  this.yy  = +this.y * 7;
  this.zz  = +this.z * 6;
  this.aa = +this.a * 5;
  this.bb  = +this.b * 4;
  this.cc = +this.c * 3;
  this.dd  = +this.d * 2;

 this.sumX  = (this.xx+this.yy+this.zz+this.aa+this.bb+this.cc);
 this.sumY  = this.sumX%11;
 this.sumZ  = 11-this.sumY;

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jQuery fails to make a POST request when the content type is set to application/json

I am utilizing jQuery version 1.10.1. My goal is to execute a POST request with the content type set to application/json. The code I have implemented is as follows: $.ajax({ type: "post", url: urlBase + "user/search", contentTy ...

When I use AJAX to load a PHP file, the function's content returns as [object HTMLDivElement]

Hello there, When I use AJAX to load a PHP file, the content of my function returns [object HTMLDivElement], but if I load my function without loading the PHP file, it displays normally. index.php <h1>API Football</h1> <nav> ...

What methods can be used to construct components using smaller foundational components as a base?

Is there a more elegant approach to constructing larger components from smaller base components that encompass common functionalities, akin to an interface in the OOP realm? I'm experimenting with this concept, but it feels somewhat inelegant. Repor ...

Effectively handle multiple connections from nodejs to postgres using the pg library

I need to run a script that performs multiple queries using the pg library for managing connections. However, I am facing an issue where my program stops working when the connection pool is full and does not queue future queries. I have tried setting the p ...

Tips for generating a JSON-driven table in Angular 2

I'm attempting to build a dynamic datagrid in angular2 using a JSON object as the source. The challenge I face is not knowing the structure of the columns within the table, making it difficult to render the rows properly. My understanding is that I n ...

The JavaScript code for summing two numbers is not functioning as expected

My HTML code takes a user input number, performs a calculation using a formula, and displays the output. The chosen input is inserted into the formula, and the result is added to the original number. However, there seems to be an issue with adding decimal ...

Is it possible to replace JavaScript files that are included in my index page if I am utilizing conditional comments specifically for IE9?

My website works perfectly in all browsers, except for IE9. After some investigation, I discovered that the issue lies with a jQuery plugin called multilevelpush.js. While it works great on other browsers, it simply won't cooperate with IE9. Upon fur ...

steps for adding text to the header with selenium webdriver

I came across the following code snippet: <body> <table> <tr> <td style= "width:30%;"> <img class = "new" src="images/new-icon.png"> <td> <h1>Hello there!</h1> ...

initiating a submission upon the occurrence of an onchange event on an input field of type "file"

I have encountered an issue while trying to submit a form using the onchange event of an input element with type file. The problem is that it submits an empty form even when a file has been chosen. Here is the code snippet: var form = document.createElem ...

JavaScript - convert the values of an array within a JSON object into separate strings

I am receiving a JSON object from an API, and my next step involves some string analysis of each key value. This process works perfectly for 90% of the objects I receive because the key values are strings. { ID: '0012784', utm_source: 'webs ...

The slides on SlickJS are initially loaded in a vertical alignment, but once the arrows are interacted with,

I've put together a demonstration that highlights the issue I'm facing. Upon visiting the contact section, you'll notice that all the slickJS slides are stacked vertically initially, but once you interact with them by clicking on the arrow o ...

Incorporating Stripe into your Next.js 13 application: A step-by-step guide

Struggling to incorporate Stripe into my Next.js 13 app for a pre-built checkout form. So far, all attempts have fallen short. Seeking assistance from anyone who has conquered this integration successfully. Any tips or guidance would be highly valued. Pl ...

The v-autocomplete feature in vuetify doesn't allow for editing the text input after an option has been selected

Link to code on CodePen: CodePen Link When you visit the provided page and enter "joe" into the search box, choose one of the two Joes that appear. Try to then select text in the search box or use backspace to delete only the last character. You will no ...

Issue with toggling in react js on mobile devices

Currently, I am working on making my design responsive. My approach involves displaying a basket when the div style is set to "block", and hiding it when the user browses on a mobile device by setting the display to "none". The user can then click on a but ...

Adding SVG to Component

I am attempting to embed an SVG element (retrieved using http.get()) into a 'icon' component. export class BgIcon { private svgSrc_: string; icon_: Icon; @Input('svg-src') set svgSrc(value: string) { this.svgSrc_ = value; ...

What is the best way to integrate a React component into an Angular project?

Struggling with integrating a React component into an Angular project and unable to make it work. I have a JavaScript file containing the React component that I want to use in Angular. Here's an example: React file... import React from "react"; c ...

Transferring information between pages in PHP

Currently, I am exploring the most effective way to pass data between two pages (Page A to Page B) using PHP for a specific scenario: Page A: This page displays a gallery of images with titles. The PHP file makes a database call to an images table, which ...

Ensuring Consistent Item Widths in a Loop using JavaScript or jQuery

In my code, I created a function that dynamically adjusts the width of elements in a loop to match the widest element among them. // Function: Match width var _so20170704_match_width = function( item ) { var max_item_width = 0; item.each(function ...

Expanding the current @types type definition to encompass additional properties that are currently absent

Currently, I am utilizing the most recent @types for leaflet in my project (v1.2.5), however, they do not align with the latest version of leaflet (1.3.x). Specifically, their LayerGroup interface lacks a setZIndex property which I need to include by exte ...

How to retrieve the first option selected in Material-UI React?

Hey there! I am currently working with React Material UI select. I have a question - how can I set the first option of items as selected without triggering the onChange method? When I change an option, it triggers the onChange method and adds an attribut ...