1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

数値判定 isNaNの罠に注意せよ!

Last updated at Posted at 2025-05-04

Paizaの文字列処理の「数値判定」問題を解いたので落とし穴と解決法を交えて解説!


問題概要

文字列 S が与えられるので、 S を整数に変換できる場合には “YES” , そうでない場合は “NO” を出力。

なお、文字列 S を数値に変換できるとは、S の全ての文字が
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } のいずれかであることをいう。

S の各文字は数字({ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } のいずれか) またはアルファベットの大文字。

入力例:
813

出力例:
YES


コード例①

const rl = require('readline').createInterface({input:process.stdin});

rl.once('line',(input)=>{
    if(!isNaN(input)){
        console.log("YES");
    } else {
        console.log("NO");
    }
});

❗ 罠ポイント

  • isNaN("")false →( Number("")0 )→ YES
  • isNaN(" ")false →( Number("")0 )→ YES
  • 指数表記(例: "123E4")も false になり、誤って YES

空白や空文字だけでも YES になる可能性があるという地味に怖い仕様。


※今回の問題は、空文字や空白はないので大丈夫。


✅ 改善コード(3段チェックで堅牢に)

const trimmed = input.trim();
const isNumber =
  trimmed !== "" &&
  !isNaN(trimmed) &&
  !isNaN(Number(trimmed)) &&
  !/[eE]/.test(trimmed);

console.log(isNumber ? "YES" : "NO");

✔ 解説(3つのチェック)

  • trimmed !== "" → 空文字の排除
  • !isNaN(trimmed) → 数値変換チェック(型変換あり)
  • !isNaN(Number(trimmed)) → 明示的に数値変換 → NaN 判定
  • !/[eE]/.test(trimmed) → 指数表記の "e" または "E" を排除

さらに、&&の短絡評価を使って、効率的にチェック!

&& は論理積(AND)演算子で、左から右へ評価を行う。
各条件が true の場合に次の条件を評価し、
最終的にすべての条件が true であれば全体が true になる。
途中で false が出た時点で評価を終了(短絡評価)。


💡 新しく学んだこと

  • trim():前後の空白を削除
  • !== "":空文字は除外
  • !isNaN(…):数値に変換できるか確認
  • 入力系の問題では「予期しない入力」に注意

✅ まとめ

空文字や空白のみの入力、数値と文字が混在する入力などを適切に除外し、正確な数値判定が可能になる!

このような細かな判定を行うことで、予期しないエラーを防ぐことができる👍




🐻 僕の失敗談と解決話!

1
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?