문자열에 0-9 및 a-f 알파벳 이외의 문자가 포함되어 있지 않으면 유효한 16진수 코드로 간주할 수 있습니다.
예:
'3423ad' is a valid hex code '4234es' is an invalid hex code
문자열을 받아 유효한 16진 코드인지 여부를 확인하는 JavaScript 함수를 작성해야 합니다.
예시
이에 대한 코드는 -
const str1 = '4234es';
const str2 = '3423ad';
const isHexValid = str => {
const legend = '0123456789abcdef';
for(let i = 0; i < str.length; i++){
if(legend.includes(str[i])){
continue;
};
return false;
};
return true;
};
console.log(isHexValid(str1));
console.log(isHexValid(str2)); 출력
콘솔의 출력은 -
false true