Skip to content

Instantly share code, notes, and snippets.

@girol
Created April 28, 2017 20:12
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save girol/4a606d5cc6286ce1e9755faa3b7746df to your computer and use it in GitHub Desktop.
Save girol/4a606d5cc6286ce1e9755faa3b7746df to your computer and use it in GitHub Desktop.
Gerando cores aleatórias com JavaScript
// gera uma cor aleatória em hexadecimal
function gera_cor(){
var hexadecimais = '0123456789ABCDEF';
var cor = '#';
// Pega um número aleatório no array acima
for (var i = 0; i < 6; i++ ) {
//E concatena à variável cor
cor += hexadecimais[Math.floor(Math.random() * 16)];
}
return cor;
}
@MarcosViniciusAlves
Copy link

chapa só transforma a var hexadecimais em array.

@girol
Copy link
Author

girol commented Jun 17, 2020

chapa só transforma a var hexadecimais em array.

Fala Marcos! Esse gist é mto antigo. Era quando eu tava aprendendo JS. E nem testei direito kkkkk

Valeu!

@girol
Copy link
Author

girol commented Jun 17, 2020

Acabei de testar aqui, dessa forma funciona. A string funciona como sequências em Python, podendo ser acessadas pelo índice.

Character Access:

There are two ways to access an individual character in a string. The first is the charAt() method:

return 'cat'.charAt(1) // returns "a"

The other way (introduced in ECMAScript 5) is to treat the string as an array-like object, where individual characters correspond to a numerical index:

return 'cat'[1] // returns "a"

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment