cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Trouble with Javascript syntaxes and if Statement

PhilipB
8-Gravel

Trouble with Javascript syntaxes and if Statement

Hi everyone, 

Having some trouble with a simple function. Doing a simple calculation function with input fields for a variable a and b and the function is supposed to return a x b. 

Having trouble though with if someone writes something else then a number in the input fields like a symbol or a letter e.g. @,f,g,% or some thing like that. Tried different variations and here is the idea of the code: 

 

$scope.calculate = function(a,b) {
    
    a = $scope.app.view.Home.wdg["textInput-1"].text; 
	b = $scope.app.view.Home.wdg["textInput-2"].text; 
  
  var q;
  q = a * b;
 
  	if (typeof q !== 'number') {
       		$scope.app.view.Home.wdg["label-2"].text = "Write numbers";
    }    
  	else{
      	$scope.app.view.Home.wdg["label-2"].text = "The value is: " + q;
  	}
}
  

 

The problem is the if statement, the idea is that if q is a datatype anything under then a number the text label should say write numbers, else it should display the value of q.

But I try this in different variations and all I get is either the value of q or if you write symbols it display "NaN". 

Someone know of to resolve this? 

1 ACCEPTED SOLUTION

Accepted Solutions

Hello, 

 

thanks for answer, tested that functionality as well but didn't really work well. It worked in preview and limited in such a way so only numbers could be written in the text field, but when publishing and used in phone it didn't work well.

Have asked around and searched the web and eventually ended up with this code that worked: 

$scope.calculate = function(a,b) {

    a = $scope.app.view.Home.wdg["textInput-1"].text; 
	b = $scope.app.view.Home.wdg["textInput-2"].text; 
  
  var q;
  q = a / b;
 
  	if (Number.isFinite(q)){
      q = q.toFixed(2);
      $scope.app.view.Home.wdg["label-2"].text = "The value is: " + q;           	
    }    
  	else{
      $scope.app.view.Home.wdg["label-2"].text = "Write integers";    	
  	}
}

View solution in original post

2 REPLIES 2

Hi @PhilipB ,

 

I think NaN means that value is undefined, and you can check first the value if it is undefined.

You can see e.g. this post (How to check a not-defined variable in JavaScript)

but I think , when I look on your code - is the quesiton what should haepend when you try to multiply some undefined things. May be some exception should be thrown and you can catch it in this case.

May be in this case you can check fist the text value of the input widget / what this  string contains : if it is numeric and could be parse to a number e.g. parseInt(), parseFload or Number() or is only string and you need to use any sting operations.

For Input widgets you can specify the type as Number with max and minimum value - so this will ensure that value could not be any other text.

 

2020-02-28_12-46-10.jpg

Hello, 

 

thanks for answer, tested that functionality as well but didn't really work well. It worked in preview and limited in such a way so only numbers could be written in the text field, but when publishing and used in phone it didn't work well.

Have asked around and searched the web and eventually ended up with this code that worked: 

$scope.calculate = function(a,b) {

    a = $scope.app.view.Home.wdg["textInput-1"].text; 
	b = $scope.app.view.Home.wdg["textInput-2"].text; 
  
  var q;
  q = a / b;
 
  	if (Number.isFinite(q)){
      q = q.toFixed(2);
      $scope.app.view.Home.wdg["label-2"].text = "The value is: " + q;           	
    }    
  	else{
      $scope.app.view.Home.wdg["label-2"].text = "Write integers";    	
  	}
}
Top Tags