2
0

同じ文字かどうかチェック

面倒くさいので画面パタメータをListでもらった後のFunctionだけ

ちなみに引数のListを作ってる箇所はこっちの記事に書いてあるYO!!

sameStr.js
// [問題文(原文)]
// 2 つの文字列 a, b が入力されます。文字列が一致していれば "OK" 、異なっていれば "NG" と出力してください。
function sameStr(lines) {
  if (!Array.isArray(lines) || lines.length !== 2) {
    console.log("2個の文字を入れろや!");
    return;
  }
  if (!lines.every(line => line.length > 0 && line.length < 101)) {
    console.log("1~100文字を入れろや!");
    return;
  }
  console.log(lines.every(line => line === lines[0]) ? "OK" : "NG");
}

module.exports = {
  sameStr
};

コレがJavaだと初心者がよくやりがちな「==」で比較して一致しない問題が合ったりするけど、JavaScriptの文字の比較は超簡単。
とりあえず小難しく書くためにeveryとか使って、配列の1つ目と全部一致かどうかという判定方法で記載してみました。
※今後の保守とかで複数来てもコレならスグに対応できるような柔軟な書き方、保守とかありえんけど

2
0
0

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
2
0