0

If the word is ABC

A[0][0]="AA"   A[0][1]="AB"   A[0][2]="AC"
A[1][0]="BA"   A[1][1]="BB"   A[1][2]="BC"
A[2][0]="CA"   A[2][1]="CB"   A[2][2]="CC"

using for, string or array method.

1
  • 2
    show us your code and what you have tried so far. Commented May 10, 2019 at 22:25

2 Answers 2

1

const a = [..."ABC"];

console.log(
  a.map(l => a.map(c => l + c))
);

Sign up to request clarification or add additional context in comments.

Comments

0

An odd request. Is this what you are looking for?

const word = "ABC";
const letters = word.split("");
const array = [];
letters.forEach((letter1,index1) => {
  letters.forEach((letter2,index2) => {
     if (!array[index1]) {
        array[index1] = [];
     }
     array[index1][index2] = letter1+letter2;
  });
});
console.log(array);

UPDATE:

Another version using older Javascript. Also, check out Asaf's solution using a more functional approach below, is very elegant.

var word = "ABC";
var letters = word.split("");
var array = [];
for(var index1 = 0;index1!==letters.length;index1++) {
  for(var index2 = 0;index2!==letters.length;index2++) {
     if (!array[index1]) {
        array[index1] = [];
     }
     array[index1][index2] = letters[index1]+letters[index2];
  }
}
console.log(array);

5 Comments

I've added a version without the forEach
Geogebra say this message: org.mozilla.javascript.Nativearray@5325dfd6
When this message appears, I must use the alert command to see the information of the array variable, but I must create a variable string (var str_ that processes it to txt so that the alert command understands it. str={{"AA","AB","AC"},{"BA","BB","BC"},{"CA","CB","CC"}}.
I already managed to do the other part of the code I wanted. Thank you so much! I cann't place here because it exceeds the characters.
That's good to hear. If you found my help useful then please upvote my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.