1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

TypeScript(JavaScript)によるカタカナ五十音順の行を判定

Last updated at Posted at 2022-08-02

はじめに

Mapオブジェクトを使っています。
キーは数字ですが、ニーズによって「第一行目」みたいな文字列に変更することができます。
下のコメント欄に他の方々からの方法もありますので、参考にしてください。みなさんありがとうございます。

コード

TypeScript Live Demo
JavaScript Live Demo

  // TypeScript

  const handleNameCode = (str: string): number => {
    // カタカナ定義
    const kana: Map<number, string> = new Map([
      [1, 'アイウエオ'],
      [2, 'カキクケコガギグゲゴ'],
      [3, 'サシスセソザジズゼゾ'],
      [4, 'タチツテトダヂヅデド'],
      [5, 'ナニヌネノ'],
      [6, 'ハヒフヘホパピプペポバビブベボ'],
      [7, 'マミムメモ'],
      [8, 'ヤユヨ'],
      [9, 'ラリルレロ'],
      [10, 'ワヲン'],
    ])

    for (let [key, value] of kana.entries()) {
      // 存在の場合、カタカナの行数のkeyを返す
      if (value.indexOf(str) !== -1) return key
    }

    // 存在しない場合、数字などの場合
    return kana.size + 1
  }

使用

  console.log(handleNameCode('')) // 2
  console.log(handleNameCode('a')) // 11
1
0
6

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?