1

I'm new to Javascript and I am begining to learn.

I need help understanding; how can I retrieve each carachter of this array.

var possibleRaysPasswords =['mobleyAndTrentonAreDead','tyrellIsElliot','dreadPirateRoberts'];

Like in these example:

 e.g: femtocell
 f
 fe
 fem
 femt
 femto
 femtoc
 femtoce
 femtocel
 femtocell

Much appreciated.

martianwars
  • 6,380
  • 5
  • 35
  • 44
  • Yes I know how to use it, but to retrieve intire elements, not every single character, like in the example. – Sérgio Afonso Mesquita Dec 13 '16 at 18:28
  • Strings are accessible like an array. Use one loop to get each element, then another loop to get each character of the string. – Carcigenicate Dec 13 '16 at 18:29
  • This has already been answered,please check this answer http://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript – Danny Dec 13 '16 at 18:35
  • This has already been answered,please check the link below http://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript – Danny Dec 13 '16 at 18:37

6 Answers6

1

If you want to get each character of each element, you may do a simple array transformation to get you an array of all the characters in all the items:

var allOfThem = arr.join('').split('');

That is: you first join all the elements into a single string. and then you split this string into array of characters. Then you can loop over it.

Vladimir M
  • 4,403
  • 1
  • 19
  • 24
0

Can you provide an example of what you've tried so far? Doing so will help us to answer any confusion you have.

For a start, let's demonstrate how to loop through each element in the array. We can declare an array the way you demonstrated:

var myArray = ["elements", "are", "pretty", "cool! "];

To loop through this array, we can simply use a for loop.

for (var i = 0; i < myArray.length; ++i) {
    console.log(myArray[i]);    // this provides the element at the ith index of the array
}

This will log, in order:

elements
are
pretty
cool! 

You can access the individual characters of a string in the same exact way that you access individual elements of an array. Try that and see if you can get to where you need to be.

0

You could use two nested loop, one for the array and one for the atrings for the letters in combination with String#slice

var possibleRaysPasswords =['mobleyAndTrentonAreDead','tyrellIsElliot','dreadPirateRoberts'],
    i, j;

for (i = 0; i < possibleRaysPasswords.length; i++) {
    for (j = 1; j <= possibleRaysPasswords[i].length; j++) {
        console.log(possibleRaysPasswords[i].slice(0, j));
    }
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Let me know if something is unclear. The comments in the code should tell you what's going on though:

// making this a constant, because we only want to read from this data
const passwords = ['mobleyAndTrentonAreDead', 'tyrellIsElliot', 'dreadPirateRoberts'];

// somewhat recently, we can also define functions like this
const printPassword = password => {
  // this function prints out the password starting from the first character, all the way to its end
  // if the password is 'test', the output should be 't te tes test'

  let resultString = ''; // this will be returned later
  // let's use the good old for loop
  // start at the first character (zero-indexed), and use the counter variable i to mark the end of the substring
  for (let i=1; i <= password.length; i++) {
    resultString += password.substring(0, i) + ' ';
  }
  return resultString;
};

// iterating over every password in the passwords array,
// and log the returned string to the console
passwords.forEach(password => console.log(printPassword(password)));
manonthemat
  • 6,101
  • 1
  • 24
  • 49
0

user 'forEach' to iterate elements of array and 'substr' to part of string:

var possibleRaysPasswords =['mobleyAndTrentonAreDead','tyrellIsElliot','dreadPirateRoberts'];
possibleRaysPasswords.forEach(function(element){
   for(var i=0 ; i<element.length ; i++){
       console.log(element.substr(0,i));
   }
});

'for of' can also be used for iteration:

for (element of possibleRaysPasswords){
   for(var i=0 ; i<element.length ; i++){
       console.log(element.substr(0,i));
   }
}
Asad Sarwar
  • 533
  • 3
  • 10
-1

As simple as that:

var letters = [];
for(let i of possibleRaysPasswords ){letters.push.apply(letters,i.split(""))}
console.log(letters);

That will create and array with all letters. Not sure if that was the question though

kimy82
  • 4,069
  • 1
  • 22
  • 25