-2

How can I generate a random URL by joining strings contained in an array (should also eliminate duplicates)?

Array of strings

let array=['flip','amazon','ebay','amazon']

expected output

['http://www.flip.com','http://www.amazon.com','http://www.ebay.com']
R.J. Dunnill
  • 2,049
  • 3
  • 10
  • 21
hari prasanth
  • 716
  • 1
  • 15
  • 35
  • 1
    The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into in a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Jun 22 '19 at 06:11
  • The resulting array doesn't seem to be random at all. – tkausl Jun 22 '19 at 06:12
  • Is it possible to generate a valid random url for an given length? @tkausl – hari prasanth Jun 22 '19 at 06:14
  • 1
    What is the code you are having trouble with? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Please, provide a [mcve]. – Jörg W Mittag Jun 22 '19 at 06:29

4 Answers4

1

You can try the following:

let array = ['flip', 'amazon', 'ebay', 'amazon']
/** Generating random number between 0 to length of array **/
let index = Math.floor(Math.random() * array.length);
console.log(`http://www.${array[index]}.com`);
double-beep
  • 5,031
  • 17
  • 33
  • 41
Pushpendra Kumar
  • 1,721
  • 1
  • 15
  • 21
  • 1
    In my opinion, with this level of a question, it's more productive to link it to another answer rather than "duplicating" an existing one – GalAbra Jun 22 '19 at 06:22
0

try this

const array=['flip','amazon','ebay','amazon']
/*
[
  'http://www.flip.com',
  'http://www.amazon.com',
  'http://www.ebay.com'
]
*/

const keywords = []
for (const keyword of array) {
  if (!keywords.includes(keyword)) {
     keywords.push(keyword)
  }
}

const result = keywords.map(item => `http://www.${item}.com`)

console.log(result)
Alongkorn
  • 3,968
  • 1
  • 24
  • 41
0

You can use Set to remove duplicates,And you can only select one random value at a time ,you just cant select whole array randomly .

let array=['flip','amazon','ebay','amazon']
let result=new Set(array.map(ele=>`http://www.${ele}.com`))
   // your expected output
console.log(Array.from(result)) // select random from here if this is what you looking for


//for getting random values 

var rand = Array.from(result)[Math.floor(Math.random() * Array.from(result).length)];
console.log(rand)

Random value generation referred From here

Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
0

First create the URLs - then use a Set to remove duplicates:

let array = ['flip', 'amazon', 'ebay', 'amazon']
const res = [...new Set(array.map(e => `http://www.${e}.com`))];
console.log(res);

You could also use reduce in two ways:

let array = ['flip', 'amazon', 'ebay', 'amazon']
const res = [...array.reduce((a, c) => (a.add(`http://www.${c}.com`), a), new Set())];
console.log(res);

Or:

let array = ['flip', 'amazon', 'ebay', 'amazon']
const res = Object.values(array.reduce((a, c) => (a[c] = a[c] || `http://www.${c}.com`, a), {}));
console.log(res);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79