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 trialtjeiosajfdksl
Courses Plus Student 8,981 Pointsneed help with code challenge
Now, use the test() method to test the string in text against the regular expression you just created. Remember the test() method returns a true or false value. Return the result of testing the string from the isValidHex method.
```// Type inside this function function isValidHex(text) { const 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)"; } }); /``` Could someone help out with this challenge please?
I know I should use : text.test(hexRegEx) but that is as all.
// Type inside this function
function isValidHex(text) {
const 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>
Brandon Benefield
7,739 PointsCharlie Gallentine There can't be a better answer because that IS the answer.
PS
You either have two accounts or you share the same name with someone else on treehouse... spooky
Charlie Gallentine
12,092 Points@Brandon Benefield Haha yeah, I had a Treehouse account last year but had to cancel it for time and budget reasons. When I came back, I forgot about the old one so now I get to be my own twin :)
1 Answer
pcachia
14,454 Points// Type inside this function
function isValidHex(text) {
var hexRegEx = /^#[a-fA-F0-9]{6}$/;
return hexRegEx.test(text);
}
This code should do fine. It asks you to test a value and return a boolean. so it's: return (boolean value) (your regex).test(on value);
Mike Siwik
Full Stack JavaScript Techdegree Student 8,483 PointsIn a previous video, it did not indicate that you could do
[a-f0-9]
I tried many different combinations like /^\(\d[a-f]\)\s\d[0-9]$/i.test(text);
And nothing worked. thank you for this
Charlie Gallentine
12,092 PointsCharlie Gallentine
12,092 PointsI'm not quite sure, but try including your regex testing statement inside the function where
// Type inside this function
Similar to :
That should cause the function to return the boolean value of calling the .test() method on "text".
Hope that helps, otherwise I'm sure someone will chime in soon with a better answer!