Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A console.log with a little more... flair.
console.rainbow = function (str) {
const colors = [
"red",
"orange",
"yellow",
"green",
"blue",
"indigo",
"violet",//white in VS Code
]
let color_counter = 0;
let color_direction = 1;
let log_array = [];
let log_string = "";
for (var i = 0; i < str.length; i++){
char = str.charAt(i)
let color = colors[color_counter];
let char_string;
if(char != " "){
if (color_counter >= (colors.length - 1)) {color_direction = -1};
if (color_counter <= 0) {color_direction = 1};
color_counter += color_direction;
let color_string = `color:${color};font-size: large;`;
char_string = `%c${char}`;
log_array.push(color_string);
} else {
char_string = char;
}
log_string += char_string;
}
log_array.unshift(log_string);
this.log(...log_array);
};
//console.rainbow("Hello Fabulous World");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.