Skip to content

Instantly share code, notes, and snippets.

@Benargee
Last active January 1, 2022 17:24
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Benargee/abf0ceba9baa92a9a2a2d0f2952b6928 to your computer and use it in GitHub Desktop.
Save Benargee/abf0ceba9baa92a9a2a2d0f2952b6928 to your computer and use it in GitHub Desktop.
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