0

I am running a forEach() loop, and I need to console.log(). But I want to get a different color for each iteration. I went trhough docs, but couldn't find anything. Is there any way possible to achieve the same?

let arr = ["a", "ab, "abc"]
arr.forEach(arr, e => {
  console.log(chalk.red(e)) //maybe something like - chalk.randColor()
})
sujeet
  • 3,480
  • 3
  • 28
  • 60

5 Answers5

2

You can try something like this:

// Create an array of possible colors
const color = ['red', 'green', 'blue', 'magenta', 'cyan', 'gray'];
let arr = ["a", "ab", "abc"]
arr.forEach(arr, e => {
  // and get a random color name from the array
  // and call the function on it
  console.log(chalk[color[Math.floor(Math.random() * color.length)]](e))
})
Zuckerberg
  • 1,781
  • 2
  • 10
  • 19
  • 1
    You will get an ````Unexpected token```` error in line 7. Remove the dot before ````[color[Math.floor(Math.random() * color.length)]](e))```` – Abito Prakash Feb 22 '20 at 08:23
1

You can define an array of strings (all supported colors)

const colors = ['red', 'blue', 'green'];

Then in each iteration get a random color and use chalk[color]

let arr = ["a", "ab", "abc"];
const getRandomColor = (str) => {
  const colors = ["red", "blue", "green"];
  console.log(chalk[colors[Math.floor(Math.random() * colors.length)]](str));
};

arr.forEach(getRandomColor);
Abito Prakash
  • 4,368
  • 2
  • 13
  • 26
  • Good, but put the chalk in color function to encapsulate all that concern in one place."getRandomChalk". Leaking that complexity will lead to potential bugs in consumers of the function. – Josh Wulf Feb 22 '20 at 08:21
  • Thanks for the feedback @JoshWulf. Moved chalk to the function – Abito Prakash Feb 22 '20 at 08:33
  • 1
    Beautiful. If you lift console.log, you could do: `arr.forEach(LogWithRandomColor)` – Josh Wulf Feb 22 '20 at 08:35
0

These two functions provide much bigger range of different colors randomly:

// Using chalk.hex()
const randColorHex = (msg) => {
  chalk.hex('#' + (Math.random() * 0xFFFFFF << 0).toString(16))(msg);
}

// Using chalk.rgb()
const randColorRgb = (msg) => {
  const rand = () => Math.floor(Math.random() * 255);
  chalk.rgb(rand(), rand(), rand())(msg);
}

// Usage
let arr = ["a", "ab", "abc"];

arr.forEach(item => console.log(randColorHex(item)));
arr.forEach(item => console.log(randColorRgb(item)));
tipos
  • 362
  • 1
  • 3
  • 13
0

you can use chalk.rgb with randomizer value to give all level of combination colors to you.

let arr = ["a", "ab", "abc"]
const rdClr = () => Math.floor(Math.random() * 255);
const randomClor = str => chalk.rgb(rdClr(), rdClr(), rdClr())(str);
arr.forEach(arr, e => {
  console.log(randomClor(arr + '%s'));
});
Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
Raj Kumar
  • 839
  • 8
  • 13
0

If you use randojs.com, the randomness can be simpler and more readable, like this:

var array = ["a", "ab", "abc"], colors = ['red', 'green', 'blue', 'magenta', 'cyan', 'gray'];

array.forEach((item, i) => console.log(chalk[rando(colors).value](i)));

If you want to use this code, just make sure this is in the head tag of your html document:

<script src="https://randojs.com/1.0.0.js"></script>
Aaron Plocharczyk
  • 2,776
  • 2
  • 7
  • 15