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 trial
Revi Nauman
60 PointsJavaScript needs fixes
Hi all teamtreehouse users! Could you help me please to modify and easy javascript code? here is the part that needs to be modified:
<script>
//blablablabla here comes the part of the script that we dont need....
answer.onresult = function (intent, entities) {
var r = entities.page.value;
var m_left = ("user wants the block to the left");
var m_right = ("user wants the block to the right");
if (r == "right") { document.getElementById("result").innerHTML = m_right; }
//bla bla bla we dont need this part of script either
</script>
<script> divBar = null;
function detectKey(event) {
//here is how the control works now. Codes for keys //97 = a //115 = s //100 = d //119 = w - this one is used for the left movements
if (event.charCode == 97) {
//a
divBar.style.left = subFromDim(divBar.style.left, 10);
}
else if (event.charCode == 115) {
//s
divBar.style.top = addToDim(divBar.style.top, 10);
}
else if (event.charCode == 100) {
//d
divBar.style.left = addToDim(divBar.style.left, 10);
}
else if (event.charCode == 119) {
//w
divBar.style.top = subFromDim(divBar.style.top, 10);
}
}
function createDiv() { divBar = document.createElement("div"); divBar.id = "divBar"; divBar.style.border = "solid 1px #AAAAAA"; divBar.style.backgroundColor = "black"; divBar.style.top = "400px"; divBar.style.left = "0"; divBar.style.height = "10px"; divBar.style.width = "100px"; divBar.style.position = "absolute"; document.body.appendChild(divBar); if (document.addEventListener) { document.addEventListener("keypress", detectKey, false); } else if (document.attachEvent) { document.attachEvent("onkeypress", detectKey); }
}
function addToDim(orig, val) { var num = parseInt(orig); return((num + val) + "px"); }
function subFromDim(orig, val) { var num = parseInt(orig); return((num - val) + "px"); }
createDiv(); </script>
////////
Ok. so, the explanation. This part :
answer.onresult = function (intent, entities) {
var r = entities.page.value;
var m_left = ("user wants the block to the left");
var m_right = ("user wants the block to the right");
if (r == "right") { document.getElementById("result").innerHTML = m_right; }
works just fine - if the value of r is "right" then we get a text inside the div "result" with the text: user wants the block to the left.
The next part is the script with the function that creates a black div "divBar"and allows to control the position of this div with the a,s,d,w keys from keybord. What do i need is to control it with the users expressons. So i need to take the value of "r" from the first script and then if ,for example r = "right" then do the function that is controled now with the "D" key from the keyboard. So this is it. Thanks fro your help guys!
3 Answers
Revi Nauman
60 PointsRihard, i mean that i need to use commands generated by the user, insted of using keys. So if the script gets the value of r == "left" i need to execute the movement to the left, automatically, and not but using "D" key
Revi Nauman
60 Pointsbtw i don know why teamtreehouse forums splited the code into different tabs :) i didnt do that
Revi Nauman
60 Pointsi`ve sold the problem. Thred could be delited
Richard Duncan
5,568 PointsRichard Duncan
5,568 PointsNO idea what you're asking but this is how I have in the past used keyboard interactions to control faux movement.
http://codepen.io/KryptoniteDove/pen/csvga
Hope that helps. It may be as simple as adding the codes for the additional keys into your logic.