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関数ドリル】初級編のjoin関数の実装アウトプット

Posted at

join関数の課題内容

_.join関数を自分で実装する課題。
https://lodash.com/docs/4.17.15#join

「課題内容」/「解説動画」/「解答例」を確認したい場合は、以下リンク先のページを参照。
https://js-drills.com/blog/join/

課題に取り組む前の状態

  • 解答例を見なくてもできそうだと思った。

課題に取り組んだ後の状態

  • 解答例は引数の配列を一旦新しい配列にコピーし、配列の最初の値を変数宣言時に入れているのが、自分の解答と主に異なる点で、そういう方法は思いつかなかったので、参考になった。

join関数の実装コード(答えを見る前)

function join(array, separator = ',') {
    let str = "";
    for (let i = 0; i < array.length; i++) {
        str += array[i];
        if (i < array.length - 1) {
            str += separator;
        }
    }
    return str;
}

join関数の実装コード(答えを見た後)

function join(array, separator = ',') {
    let str = array[0];
    for (let i = 1; i < array.length; i++) {
        str += separator + array[i];
    }
    return str;
}
0
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
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?