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

javascriptメモ

Last updated at Posted at 2023-02-26

初心者による自分用メモです


2以上の自然数の入力について、
入力値が素数ならtrueを、合成数ならfalseを返す関数

primeChecker
let primeChecker = (num) => {
    for (let i=2; i<=parseInt(Math.sqrt(num)); i++) {
        if (num%i==0) {
            return false;
        }
    }
    return true;
}

2以上の自然数の入力について、
入力値が合成数なら0を、素数なら入力値を返す関数

primeEmitter
let primeEmitter = (num) => {
    for (let i=2; i<=parseInt(Math.sqrt(num)); i++) {
        if (num%i==0) {
            return 0;
        }
    }
    return num;
}

インデックスが大、小の順で指定された場合、文字を反転して出力する関数
0以上のインデックスのみを想定しています。

Slice
let Slice = (str, a, b) => {
    if (a <= b) {
        return str.slice(a, b);
    } else {
        let revStr = '';
        let list = [];
        for (let i=a-1; i>=b; i--) {
            revStr += str.charAt(i);
        }
        
        return revStr;
    }
};
0
0
2

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