'Javascript: Get nth character of string (JSChallenger)
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!
Solution 1:[1]
Try this one
function myFunction(a, n){
return a[n - 1];}
Solution 2:[2]
// 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);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mike Ligdo |
Solution 2 | Bigya Dahal |