最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: Get nth character of string (JSChallenger) - Stack Overflow

programmeradmin8浏览0评论

I am a total newbie and currently learning Javacript. I encountered this problem on JSChallenger and have been struggling with it. Here's my code:

// Write a function that takes a string (a) and a number (n) as argument
// Return the nth character of 'a'
function myFunction(a, n)
{let string = a;
let index = n;
return string.charAt(index);
}

Can anyone point out my errors? Thanks so much!

I am a total newbie and currently learning Javacript. I encountered this problem on JSChallenger and have been struggling with it. Here's my code:

// Write a function that takes a string (a) and a number (n) as argument
// Return the nth character of 'a'
function myFunction(a, n)
{let string = a;
let index = n;
return string.charAt(index);
}

Can anyone point out my errors? Thanks so much!

Share Improve this question asked Feb 26, 2022 at 9:18 eight_tea_babieight_tea_babi 11 gold badge1 silver badge1 bronze badge 6
  • Its working fine. I did not see any error in this code. Please refer this fiddle jsfiddle/02gs5dby – Rohìt Jíndal Commented Feb 26, 2022 at 9:20
  • 1 Seems to work just fine. What's the problem? (BTW you can skip creating two additional variables and just return a[n]) – Liftoff Commented Feb 26, 2022 at 9:20
  • it works fine but did you call the function? add this bellow your function myFunction(string, number); and change the parameters to suit your case. – Mohammed Alwedaei Commented Feb 26, 2022 at 9:23
  • "Can anyone point out my errors?" What errors? What is telling you something is wrong? The site? If so, it's probably plaining about string and index -- they're pletely unnecessary. Alternatively, it could be plaining about not handling multi-code-unit code points in the string (my blog post with details here), but that's very unlikely. (If that were the problem, a solution to it would be for (const ch of a) { if (n-- === 0) { return ch; } } or alternatively return [...a][n];). – T.J. Crowder Commented Feb 26, 2022 at 9:23
  • It's working fine. Just note that array index starts at 0. So n=0 will return first character. – Rupam Commented Feb 26, 2022 at 9:24
 |  Show 1 more ment

11 Answers 11

Reset to default 2

Try this one

function myFunction(a, n){
return a[n - 1];}

Here's a very short solution

function find( a, b){
return a[b]
}
console.log(find('how',1));

although you need to do some tests in the function to check if the input for b is greater than the length of a else it would fail.

You can do that like this

function find( a, b){
    if(a.length<b-1){
        return 'error';
    }
    return a[b]
}
console.log(find('how',5));

that way your code would be free from error

// there is a shorter way to do this since you want to find what's missing here is what's missing. 
function myFunction(a, n){
    let string = a;
    let index = n-1;
    return string.charAt(index);
}

JS string's index starts enumerating from 0 so the n should be decremented by 1

// Write a function that takes a string (a) and a number (n) as argument
// Return the nth character of 'a'
function myFunction(a, n)
{
    let string = a;
    let index = n;
    return string.charAt(index-1);
}

the easiest way to write it would be:

// Write a function that takes a string (a) and a number (n) as argument
// Return the nth character of 'a'

function myFunction(a,n) {
   return a[n - 1];
}

Meaning return from string "a" of myFunction the index "[n-1]" , "n-1" it's a needed operation to get the right index because string index start enumerating from 0.

It is very easy you can just return the a[n] but it is not the right answer because as you know the strings in JavaScript are zero-indexed so the right answer is

function myFunction(a, n) {
    return a[n - 1]
}

Happy Coding & keep Learning

also this solution works too, with empty space at first of string we can remove that counting from zero :

function myFunction(a, n) {   
   return ` ${a}}`[n]
}

but this is just another solution. not tested. and you can check this repo for other JSchallenger solutinos: JSchallenger solutions

in javascript, you can write this simple code to solve your problem ex:

let string = "Hello"; let n = 2;

return string[n];

function myFunction(a, n) {
  return a.charAt(n - 1);
}
console.log(myFunction("hello", 2));

I think so this is best way

that is a tricky question if you look to the test cases you will notice that it ignore counting from zero

 function myFunction(a, n) {
   return a.charAt(n-1);
 }

Method 1:

function myFunction(a, n){
  return a[n - 1];
} 

Method :2

function getNthCharacter(a, n) {
  return a.charAt(n - 1);
}

Answer for the above question is

function myFunction(a, n) {
  return a[n-1]
}

Above can be solved as

    function myFunction(a, n) {
      return a.charAt(n-1)
    }
发布评论

评论列表(0)

  1. 暂无评论