JavaScript- How do I return the character at that index without using charAt method

前端 未结 4 1593
忘掉有多难
忘掉有多难 2021-01-16 17:57

I have a function that accept two parameters string and index. How do I write a code that will return the character at that index without using javascript built in method ch

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-16 18:38

    Something like this should work for you:

    getIndex = (str, index) => str.split('')[index];
    console.log(getIndex("foobar", 3));

    This may also be of interest:

    String.prototype.getIndex = function(idx) {
      return this.split('')[idx];
    }
    console.log("foobar".getIndex(3));

    Hope this helps,

提交回复
热议问题