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);
});
}
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.
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: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.Ah, clever! Thanks for the code example!