2

私がこれを持っていると仮定して:

var a = parseInt(prompt("Enter A"));
var b = parseInt(prompt("Enter B"));
var c = parseInt(prompt("Enter C"));

3つの変数すべてに値が存在するかどうかを確認する最短の方法は何ですか?

行うこと if(a && b && c) は正確ではありません原因0は偽りです。

ps a、b、cはintまたはstringsにすることができます...また、ここにはトリックが必要です...

文字列の例の場合:

var a = (prompt("Enter A"));
var b = (prompt("Enter B"));
var c = (prompt("Enter C"));
4

6 に答える 6

2

タイプに関係なく、これらの値がすべて定義されているかどうかを分類的に確認する唯一の方法は、次のとおりです。

if( (typeof a !== 'undefined') || 
    (typeof b !== 'undefined') || 
    (typeof c !== 'undefined')) { /* ... */ }

残念ながら、値の真実性に依存するショートカット、そしてあなたが指摘するように、これは常に受け入れられるアプローチではありません。

Alex Kが示すように、変数の型を事前に知っていれば、いくつかのトリッキーなことを行うことができますが、型を事前に知っていると、比較で避けるのと同じくらい多くのコードが必要になる場合があります。

編集1

promptDave Newtonは、常に定義された値を返すことを指摘しました。nullユーザーがキャンセルを押すと戻ります。

したがって、より良いアプローチは次のようになります。

function isValid(value) {
    return typeof value !== 'undefined' && value !== null);
}

if( isValid(a) && isValid(b) && isValid(c) ){ /* ... */ }

編集2

もちろん、最も簡単な方法は、intを解析する前に有効性を確認することです。以来

!!""  === false
!!"0" === true
!!0   === false

プロンプトが返す文字列/nullで使用でき(a && b & c)、それらが有効な場合にのみ解析を実行します。

于 2013-01-04T15:32:50.643 に答える
1

これはどう:

ok = !isNaN(a+b+c);
于 2013-01-04T15:33:01.670 に答える
0

やってみました(!!a && !!b && !!c)か?

AFAIK, unless the value of a string variable is empty, null or undefined it should evaluate to true. 0 would also return false but I'm writing on behalf of your prompt() call since its value would not be of type Number.

于 2013-01-04T15:53:00.697 に答える
0

Always use:

if (typeof( variableName ) != "undefined") {
    //Do something
}

Since Javascript uses Json object notation the following code is completely valid:

var x = {};

if (x.test == undefined) {
    //Do something
}

But if "x" does not exists this produces an error. There is no way to do this safer in Javascript.

于 2013-01-04T15:55:55.167 に答える
0

The shortest way to check if a set of variables has valid, acceptable values is to create a helper function according to your specification which accepts a variable number of arguments. In this example, this function will return false if an argument is undefined, null, or a NaN number. Strings will return true for isNaN, so we check if the argument's type is a number before checking if it is NaN.

function isValid() {
    var i, args = Array.prototype.slice.call(arguments, 0);
    for(i = 0; i < args.length; i += 1) {
        if( typeof args[i] === 'undefined' || args[i] === null
        || (typeof args[i] === 'number' && isNaN(args[i]))
        ) {
            return false;
        }
    }
    return true;
}

Utilize it like so:

if( isValid(a, b, c) ) {
    ...
}
于 2013-01-04T16:10:45.480 に答える
0

If the values can only be strings or numbers, you can use:

var valid = ('' + a) && ('' + b) && ('' + c); //always convert into string

Here's also a fancy but relatively short one, which deals with undefined, null and empty string:

var valid = [a].join() && [b].join() && [c].join();

In Array.prototype.join(), when an element is undefined or null it will be converted into an empty string.

But it's neither elegant nor efficient. :(

于 2013-01-04T17:16:44.177 に答える