0

I want to return the string 'street' 5 times and for it to add 4 random letters from the 'letters' array for each time so that they're different for each street.

let letters = ['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'];

let a = {
    street: 'Street 21 ',
    do: function() {
        for (let i = 0; i < 4; i++) {
            this.street += letters[Math.floor(Math.random() * letters.length)];     
        };
    }
}

My code adds 4 random letters from the array to street already, but im not sure how to duplicate it multiple times, I can't just use .repeat() as I also need it to be in new lines.

Expected output:

Street 21 JIDX
Street 21 UYXZ
Street 21 ABCD
Street 21 EFGH
Street 21 KLMN
2
  • @MichaelBianconi yes just added it Commented Aug 15, 2019 at 16:24
  • Is that output a string or array of strings? Commented Aug 15, 2019 at 16:27

3 Answers 3

1

I'd use map and join. That's easier to read, IMHO, and more functional.

const letters = ['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'];

const result = new Array(5)
  .fill('Street 21')
  .map(
    el =>
      el +
      ' ' +
      new Array(4)
        .fill('')
        .map(el => el + letters[Math.floor(Math.random() * letters.length)])
        .join('')
  )
  .join('\n');
console.log(result);

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

2 Comments

And how can i make for each line it adds 3 random letters from the letters array instead of just one?
Ahh, sorry, didn't see that you needed three letters added.
1

Considering that you want the output as a string:

let letters = ['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'];

let a = {
    street: 'Street 21 ',
    do: function() {
        var fullString = "";
        const multipleAmount = 5; // the amount of streets you want to generate.
        let streetString = this.street;

        for (let j = 0; j < multipleAmount; j++) {
           for (let i = 0; i < 4; i++) {
               streetString  += letters[Math.floor(Math.random() * letters.length)];     
           };
           fullString += streetString;
           fullString += "\n"; // new line
           streetString = this.street;
        }
        this.street = fullString;
    }
}

2 Comments

Just tried running that and this is the output i received: Street 21 FStreet 21 AStreet 21 QStreet 21 Z Street 21 ZStreet 21 IStreet 21 BStreet 21 K Street 21 KStreet 21 IStreet 21 XStreet 21 J Street 21 UStreet 21 TStreet 21 LStreet 21 B Street 21 HStreet 21 QStreet 21 QStreet 21 T 5 lines of street 21 repeated 4 times in each line, not sure how to show it properly in comments
@vcosiekx my bad, fixed now :)
1

const letters = [
  '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'
];

const street = 'Street 21 ';

function generateRandomLetter() {
  return letters[Math.floor(Math.random() * letters.length)];
}

function generateNRandomLetters(n) {
  return Array(n)
    .fill()
    .map(generateRandomLetter)
    .reduce((s, l) => `${s}${l}`,'');
}

function generateResult() {
  const getStreetValue = () => `${street}${generateNRandomLetters(4)}`       

  return Array(4)
   .fill()
   .map(getStreetValue)
   .reduce((r, s) => `${r}\n${s}`, getStreetValue());
}

const result = generateResult();

console.log(result);

Comments

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.