Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need multiple values from for loop

I want to repeat the for loop which get us new values every time.
Whenever I will run the for loop I should get the values different.
Please solve my problem

let hex = 'ABCDEF0123456789';
let res = '';
for (let i = 1; i <= 6; i++) {
    res += hex.charAt(Math.floor(Math.random() * hex.length));
    // console.log(i);
} 

I want to get new values every time when I call or run the for loop.
For Example – If I print the value of res multiple times. Every time
I want the value different

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Did you mean that you want to put your code in a function an call the function?

function res() {
  let hex = 'ABCDEF0123456789';
  let res = '';
  for (let i = 1; i <= 6; i++) {
    res += hex.charAt(Math.floor(Math.random() * hex.length));
  }
  return res;
}

console.log(res()) // => one value
console.log(res()) // => another value
console.log(res()) // => a third value
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading