Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

313
Views
I dont know how to give each value of data from a variable to a value from another variable

const people = [{
    name: 'frank',
    age: 20,
    position: 'front-end-developer',
    value: 7
  },
  {
    name: 'craig',
    age: 27,
    position: 'back-end-developer',
    value: 7
  },
  {
    name: 'peter',
    age: 23,
    position: 'database-manager',
    value: 6
  },
  {
    name: 'andy',
    age: 29,
    position: 'full-stack-developer',
    value: 9
  }
];

const salaryGenerator = (person) => {
  return '$' + (person.value * 10 / 5 * 15 + 200)
};

const sentenceGenerator = (person) => {
  return person.name + ' is ' + person.age + ' who works as a ' + person.position
}

const userNameGenerator = (person, ) => {
  const randomWord = ['donec', 'pellentesque', 'facilisis', 'potenti']
  return person.name + (person.age * person.value + 300) + randomWord[Math.floor(Math.random() * randomWord.length)]

}

const salary = people.map(salaryGenerator)
const sentence = people.map(sentenceGenerator)
const userName = people.map(userNameGenerator)
console.log(salary)
console.log(sentence);
console.log(userName);

I want to give the username a meaningless word at the end which is in randomWord. I would like it to be like frank440donec and give each username a different meaningless word at the end, but it gives all the names a different meaningless word except for two which are assigned the same meaningless word.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If the aim is to randomly assign -- avoiding duplicates -- from a set of names, then a good solution is to shuffle the names...

// names must be at least the length of the people array
const names = ['donec', 'pellentesque', 'facilisis', 'potenti'];
shuffle(names);

const userNameGenerator = (person, index) => {
  const randomWord = names[index];
  const ageXValue = person.age*person.value+300;
  return `${person.name}${ageXValue}${randomWord}`;
}

// shuffle based on  https://stackoverflow.com/a/12646864/294949
function shuffle(array) {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    const temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }
}

const people = [{
    name: 'frank',
    age: 20,
    position: 'front-end-developer',
    value: 7
  },
  {
    name: 'craig',
    age: 27,
    position: 'back-end-developer',
    value: 7
  },
  {
    name: 'peter',
    age: 23,
    position: 'database-manager',
    value: 6
  },
  {
    name: 'andy',
    age: 29,
    position: 'full-stack-developer',
    value: 9
  }
];

console.log(people.map(userNameGenerator))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!