skip to Main Content

Write a function that takes two arrays as parameters. The first array will be an array of people’s names, and the second array will be the alphabet. Using a for loop within a for loop, create and return array that looks like this:

function forception(people, alphabet){
 // your code here
}

var people = ["Jon", "Jacob", "Jingle", "Heimer", "Schmidt"];
var alphabet = "abcdefghijklmnopqrstuvwxyz";

Expected Output:

["Jon:", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Jacob:", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Jingle:", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Heimer:", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "Schmidt:", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

Here is my attempt:

var people = ["Jon", "Jacob", "Jingle", "Heimer", "Schmidt"];
var alphabet = "abcdefghijklmnopqrstuvwxyz";
const finalArray = [];
function forception(people, alphabet) {
  const alphabetArray = alphabet.toUpperCase().split("");

  for (const name of people) {
    for (let i = 0; i < people.length; i++) {
      finalArray.push(name + alphabetArray);
    }
  }
  console.log(finalArray);
}

forception(people, alphabet);

But this produces:

['JonA,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z', 'JonA,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z', 'JonA,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z', 'JonA,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,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q,R,S,T,U,V,W,X,Y,Z', 'SchmidtA,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z', 'SchmidtA,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z', 'SchmidtA,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z', 'SchmidtA,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z']

2

Answers


  1. Key point for this answer is the second last line of given solution below:-

    var result = forception(people, alphabet).join(', ');
    

    This Join function takes the array output and joins all its elements into a single string, using commas as separators. Complete code is below

    function forception(people, alphabet) {
        var output = [];
        for (var i = 0; i < people.length; i++) {
            output.push(`"${people[i]}:"`);
            for (var j = 0; j < alphabet.length; j++) {
                output.push(`"${alphabet[j].toUpperCase()}"`);
            }
        }
        return output;
    }
    
    var people = ["Jon", "Jacob", "Jingle", "Heimer", "Schmidt"];
    var alphabet = "abcdefghijklmnopqrstuvwxyz";
    
    var result = forception(people, alphabet).join(', ');
    console.log("[" + result + "]");
    Login or Signup to reply.
  2. In your code, the following line is your issue:

    finalArray.push(name + alphabetArray);
    

    You’re joining name and alphabetArray into one value and adding it to finalArray.

    Array.push() allows you to push multiple items:

    finalArray.push(name + ":", alphabetArray)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search