eight_tea_babi
eight_tea_babi

Reputation: 1

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!

Upvotes: 0

Views: 11073

Answers (11)

Aadil Rana
Aadil Rana

Reputation: 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)
    }

Upvotes: 0

Md Nazmus Shakib
Md Nazmus Shakib

Reputation: 1

Method 1:

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

Method :2

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

Upvotes: 0

fatma abdoun
fatma abdoun

Reputation: 1

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);
 }

Upvotes: 0

Favour okechukwu
Favour okechukwu

Reputation: 77

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

Upvotes: 2

Fatima Gohar
Fatima Gohar

Reputation: 1

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

I think so this is best way

Upvotes: 0

Harsh Vyas
Harsh Vyas

Reputation: 1

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

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

return string[n];

Upvotes: 0

CyrusKabir
CyrusKabir

Reputation: 158

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

Upvotes: 0

Ayubxon Qodirxonov
Ayubxon Qodirxonov

Reputation: 1

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

Upvotes: 0

Erakliton
Erakliton

Reputation: 1

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.

Upvotes: 0

Bigya Dahal
Bigya Dahal

Reputation: 1

// 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);
}

Upvotes: 0

Mike Ligdo
Mike Ligdo

Reputation: 21

Try this one

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

Upvotes: 2

Related Questions