I
I
Ily4farmer2020-04-30 22:10:02
JavaScript
Ily4farmer, 2020-04-30 22:10:02

Why is the condition in the loop not being met?

function chooseOptExpenses() {
    for (let i = 0, c = 2; i < c; i++) {
        let expenses = prompt("Введите количество необязательных статей расходов в этом месяце", '');
        console.log(expenses);
        if (typeof (expenses) === isNaN && typeof (expenses) == null
            && expenses == "") {
            c++;
            alert("Введите количество расходов", '');
        } 
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Denis Ineshin, 2020-04-30
@IonDen

isNaN(expenses) // правильно
isNaN is a function, not a type.

H
hzzzzl, 2020-04-30
@hzzzzl

typeof prompt('aaa') will always be 'string', you don't even need to check anything there

I
IlonMusk, 2020-05-01
@IlonMusk

good morning.
Do you need to make sure that the entered value is a number?
If so, then it is easier for you to set this condition.
if (typeof +expenses !== 'number' ) {
//...
}
where "+" is a cast to Number type. You can use parseInt(n);
Now, in principle, logically:
Let's say that your condition checks for the NaN type (and not the isNaN (n) function) and typeof returns not a string, then I want to pay attention to the logical operator && (AND), which, if it matches the condition, will return false , since NaN !== null && (and) null !== 'empty string',
so in each condition you check the result of typeof (...) - i.e. a string with data type other than string (returned by typeof )
The curly brace closing the body of the function is missing.
As I understand it, you wanted to call sequentially and write down two values ​​\u200b\u200btransmitted by the user, in this case you can do without a loop.
Promise || conditional statements.
That is, when you click, you offer to write down the value, process it (successful case and unsuccessful case) and then checking whether the first step has been taken, you offer to write the second value again into a new variable. Thus, you have 2 values ​​in different variables, which you can later operate as you need.

K
Karpion, 2020-05-01
@Karpion

In general, working through alerts and prompts is a so-so idea. It is better to do this through input forms with fields.
At first glance, logically - instead of &&putting it ||- try it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question