If statement displaying a NaN message instead of custom alert

Related searches

I have an if statement that checks to see if a number that the user enters is a number between 0 and the last index of an array. When I enter something other than a number, instead of going to the "else" portion of the code and displaying the alert message, it displays "NaN".

$("#delete_task").click(function() {   
        var taskIndex = prompt("Please enter the index number of the task to delete.",0);
        if(!isNaN(taskIndex) && taskIndex >= 0 && taskIndex < tasks.length) {
            tasks.sort();
            tasks.splice(taskIndex, 1);
            displayTaskList();
        } else {
            alert("Please enter a number between 0 and " + tasks.length-1 + ".");
        }
    });

prompt() always returns a string.

Subtraction will attempt to coerce strings into numbers, so one way to correct this is by just ensuring that happens before the addition (concatenation) in the rest of the alert:

let tasks = {length: "5"}

alert("Please enter a number between 0 and " + (tasks.length-1) + ".");

Making decisions in your code — conditionals, In a weather app, if it is being looked at in the morning, show a In this article, we'll explore how so-called conditional statements work in JavaScript. if ( condition) { code to run if condition is true } else { run some other code instead } 0 , NaN , or an empty string ( '' ) actually returns true when tested as a� The NaN property represents "Not-a-Number" value. This property indicates that a value is not a legal number. The NaN property is the same as the Number.Nan property. Tip: Use the isNaN() global function to check if a value is a NaN value.

The problem is because you have a String, you need convert taskIndex in Number with Number(taskIndex)

Execute functions in TradingView's if/else: how? � Kodify, TradingView's if statements execute code based on a condition. That plot should only show on Monday, so we place the plot() function Instead we get a programming error: the code blocks of if statements, but also the body of custom functions. This makes an alert condition for bars that close higher:. JD Sartain is a technology journalist from Boston. She writes the Max Productivity column for PCWorld, a monthly column for CIO, and regular feature articles for Network World.

Because you want to use the entered value as a number, while prompt() always returns a string, you need an extra step in there to convert the prompt response to a number:

$("#delete_task").click(function() {   
    var taskIndex = prompt("Please enter the index number of the task to delete.",0);
    taskIndex = Number(taskIndex); // or +taskIndex
    if(!isNaN(taskIndex) && taskIndex >= 0 && taskIndex < tasks.length) {
        tasks.sort();
        tasks.splice(taskIndex, 1);
        displayTaskList();
    } else {
        alert("Please enter a number between 0 and " + (tasks.length-1) + ".");
    }
});

The isNaN() check will still work, because if you type "foo" into the prompt dialog the conversion will result in NaN. But after conversion to a number, the comparisons will work properly as numeric comparisons instead of string comparisons.

edit — another bug: in the error alert() call, you have to parenthesize tasks.length-1. If you don't, the expression in the call is interpreted as if it were written

(("Please enter a number between 0 and " + tasks.length) - 1) + "."

The subtraction operator always attempts to convert its arguments to numbers, and clearly the result of the initial string addition will end up being NaN. I've fixed the example code.

Conditional branching: if, '?', To do that, we can use the if statement and the conditional operator ? A number 0 , an empty string "" , null , undefined , and NaN all else if (year > 2015) { alert( 'Too late' ); } else { alert( 'Exactly!' ); } else if (age < 18) { message = 'Hello! Instead, we execute different code depending on the condition. I have a question to display Infinity & NaN as 2 different values. Im using matrix to dislay gap (%) between actual volume & forecast volume. And I need to do conditioning format to display gap as below: If gap = Infitity, display "+" If gap = NaN, dispay "-" I tried to use Divide but both Infinity & NaN value are display blank.

How to use IF function in Excel: examples for text, numbers, dates , In case you don't want your Excel IF statement to display any value when the condition is met, enter� When a message statement in the C/AL code is executed, the message is not immediately displayed. Instead, it is displayed after the C/AL code is finished executing or after the C/AL code pauses to wait for user interaction. The window is automatically sized to hold the longest line of text and the total number of lines.

Conditional execution and Acrobat JavaScript, In this tutorial, learn about Conditional Execution, one of the most important The first rule of writing conditional statements is to clearly and simply express getField("AgreeCheck").value; if( cAgree == "Off" ) app.alert("You must not only sets the discount, but also displays a popup message to the user. This function returns true if the value equates to NaN. Otherwise it returns false. This function is different from the Number specific Number.isNaN() method. The global isNaN() function, converts the tested value to a Number, then tests it.

I created a new field, Cost/Quantity, and the output is coming up at NaN. I was able to see the Cost by year on a bar chart and quantity by year on a bar chart fine, but when I tried to doing Cost/Quantity on a bar chart, I am not getting anything due to NaN values.

Comments
  • isNaN() is not enough. You have to actively convert the value to a number before comparing it to other numbers; prompt() always returns a string.
  • So I would do var taskIndex = parseInt(prompt("Please enter the index number of the task to delete.",0))?
  • I am not a fan of parseInt() because it allows strings like "123foo" without error. However otherwise it would work.
  • Thanks for this, I had always thought that parenthesis were only necessary if there were multiple arguments. I thought that parenthesis were optional with simple arithmetic expressions.
  • Parentheses control the order of operations, just like in math equations. I expanded the answer a bit to explain what's going on there in more detail.
  • I added the line of code, but it still displays "NaN" instead of the alert I specified.
  • What exactly does displayTaskList() do?
  • Basically an array of "tasks" is displayed in a list, and the delete function (listed above) deletes that task from the list and from the array. displayTaskList() just re-displays the list in that array with the index specified removed from the array. It doesn't display any alerts.
  • I tested something and apparently "tasks.length-1" was the problem, I had to put it in parenthesis.
  • @coreynj yes exactly!