Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialivanbalakirev
6,643 PointsFor some reason, code doesn't work: Bummer! Your regular expression matches CSS hexadecimal color values, but also match
Hi,
Can't make it work, can someone help me out?
// Type inside this function
function isValidHex(text) {
var hexRegEx = /#[a-f0-9]{6}/i;
}
const hex = document.getElementById("hex");
const body = document.getElementsByTagName("body")[0];
hex.addEventListener("input", e => {
const text = e.target.value;
const valid = isValidHex(text);
if (valid) {
body.style.backgroundColor = "rgb(176, 208, 168)";
} else {
body.style.backgroundColor = "rgb(189, 86, 86)";
}
});
<!DOCTYPE html>
<html>
<head>
<title>DOM Manipulation</title>
</head>
<link rel="stylesheet" href="style.css" />
<body>
<div id="content">
<p>Enter a valid hex value below to make the screen turn green.</p>
<input type="text" id="hex">
</div>
<script src="app.js"></script>
</body>
</html>
8 Answers
ivanbalakirev
6,643 PointsMade it work with the following example: var hexRegEx = /^#[a-f0-9]{6}$/i;
Cheers!
Rhys Kearns
4,976 PointsOh good job! :)
Rhys Kearns
4,976 PointsYou are passing text into the function but you are simply doing nothing with it - im not sure how you are verifying that text fits with your regex verification. Maybe you need to break it down and go step by step, tell me if you need anymore help :)
ivanbalakirev
6,643 PointsHere's the task:
For this step of the challenge, write a regular expression that will match any hexadecimal string and store it in a variable named hexRegEx. The regex should be case-insensitive. Assume that the string will always be 7 characters.
Have I misunderstood smth?
ivanbalakirev
6,643 PointsThen it says Bummer! The variable hexRegEx is not declared inside the isValidHex function.
Any thoughts?
Rhys Kearns
4,976 PointsYeah that was my bad ignore my last comment! I didnt check the challenge
Rhys Kearns
4,976 PointsBummer! Your regular expression matches CSS hexadecimal color values, but also matches other values. See if you can limit your regex to meet the requirements.
Is the error message, I haven't watched the video so im not quiet sure what you learnt before this challenge :)
ivanbalakirev
6,643 PointsYes, but how to limit it? I checked my code using different online regex tools, like this one: https://regex101.com/ and it seems to work, so I'm a bit lost here.
Rhys Kearns
4,976 PointsI actually can't figure out why it isnt working... Starting to annoy me now haha
ivanbalakirev
6,643 Pointsivanbalakirev
6,643 PointsHere's the task:
For this step of the challenge, write a regular expression that will match any hexadecimal string and store it in a variable named hexRegEx. The regex should be case-insensitive. Assume that the string will always be 7 characters. I assigned a regex to the var but it doesn't pass the test, what I'm doing wrong?