DEV Community


Discussion on: What I've learned from the Advent of Code so far (days 1-5)

matrixx profile image
Saija Saarenpää

Great post! I haven't had time to participate in the Advent of Code, but I still occasionally enjoy reading from others doing the tasks. I have one suggestion. Not necessarily an improvement, but an alternative way for the for loop for checking the hex code validity. This is also using the includes Ilê Caian already mention about:

function hexValidity(hexValue) {
  let validChars = Array.from('0123456789abcdef');
  let colourArray = Array.from(hexValue);

  if (colourArray[0] != '#' || colourArray.length != 7) {
    return false;

  return colourArray.slice(1).every(character => {
    return validChars.includes(character);
Enter fullscreen mode Exit fullscreen mode

The slice function gives a sub array starting from the index 1, so it will omit the # sign. every function for the array iterates over every character of the array and returns true if all of the iterations return true. In case any of the iterations return false, the whole function returns false.

minna_xd profile image
Minna Nurmiluoto Author

Ah, clever! Thanks for the code example!