2

I am new to JS and am trying to understand chartAt. I created a problem where I want to go through an array and pull the first character of each value from my array using charAt. I'm a bit stuck with my code.

var myArray = ['adam', 'bianca', 'cat', 'dennis'];
var myFunc = function (letter) {
    for (var i = 0; i < letter.length; i += 1) {
        letter.charAt(0);
        console.log(letter.charAt(0));
    }
}
jstone
  • 435
  • 3
  • 8
  • 18

5 Answers5

10

In your iteration loop, letter is the array passed to the function myFunc(). You need to access its elements, which you're iterating via i. Use letter[i].charAt(0) instead of letter.charAt(0)

var myArray = ['adam', 'bianca', 'cat', 'dennis'];
var myFunc = function (letter) {
    for (var i = 0; i < letter.length; i += 1) {
        // Use the index i here
        console.log(letter[i].charAt(0));
    }
}

// Call your function, passing in the array you defined:
myFunc(myArray);
// a
// b
// c
// d

So your understanding of String.prototype.charAt() is correct, but the loop iteration was faulty.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • What does .prototype mean? I am seeing that by others, but not clear on what it is and when I should use it. – jstone Jan 01 '14 at 01:32
  • 1
    Have a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain JavaScript object inheritance does not operate on classes like many other languages, rather prototype methods which are shared by all instances of an object type. – Michael Berkowski Jan 01 '14 at 01:46
  • 1
    @jstone Some good stuff here too: http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work – Michael Berkowski Jan 01 '14 at 01:47
1

If what you want to do is go through each element in the array and get the first letter, you need to iterate over the array, not the word (which you call letter in your solution. So you would have something like this:

for( var i=0; i<myArray.length; i++ ) {
    console.log( myArray[i].charAt(0) );
}

Or:

myArray.forEach( function(word){
    console.log( word.charAt(0) );
});

Also, you're creating a function (myFunc), but then you're never actually invoking it.

Ethan Brown
  • 26,892
  • 4
  • 80
  • 92
0

Seems about right, except that you coded a constant in your loop instead of using the loop variable:

var myArray = ['adam', 'bianca', 'cat', 'dennis'];
var myFunc = function (letter) {
    for (var i = 0; i < letter.length; i += 1) {
        letter.charAt(i);
        console.log(letter.charAt(i));
    }
}


> myFunc(myArray[1])
b VM622:6
i VM622:6
a VM622:6
n VM622:6
c VM622:6
a VM622:6
undefined  

You might also want to print out the whole array:

for (var word in myArray) {
    word = myArray[word];
    console.log("");
    myFunc(word);
} 
Steve Clanton
  • 4,064
  • 3
  • 32
  • 38
0

if you using jQuery

var myArray = ['adam', 'bianca', 'cat', 'dennis'];
$.each(myArray,function(){console.log(this.charAt(0))});
soredive
  • 795
  • 1
  • 9
  • 25
0

If you are using ES6

const myArray = ['adam', 'bianca', 'cat', 'dennis'];
myArray.forEach(myFunc => console.log(myFunc.charAt(0)));

//output
a
b
c
d

If you want to put the output in an array

const myArray = ['adam', 'bianca', 'cat', 'dennis'];
const myFunc = myArray.map(name => name.charAt(0));  

console.log(myFunc); 
//output
[ 'a', 'b', 'c', 'd' ]

// When .map() is called on an array, it takes an argument of a callback function and returns a new array

ScriptDev
  • 1
  • 4