# So i actully thougt Javascript were easier but nope it insn't

**URL:** <https://talk.code3dgames.com/t/so-i-actully-thougt-javascript-were-easier-but-nope-it-insnt/302>\
**Category:** Uncategorized\
**Created:** [June 29, 2021, 5:16pm UTC](https://talk.code3dgames.com/t/so-i-actully-thougt-javascript-were-easier-but-nope-it-insnt/302 "2021-06-29T17:16:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin](https://talk.code3dgames.com/user_avatar/talk.code3dgames.com/martin/32/41_2.png) [@Martin](https://talk.code3dgames.com/u/Martin)\
**Post date:** [June 29, 2021, 5:16pm UTC](https://talk.code3dgames.com/t/so-i-actully-thougt-javascript-were-easier-but-nope-it-insnt/302/1 "2021-06-29T17:16:59Z")

</div>

So well i am now at the end of Chapter 6 and my Character won’t stop swinging his Arms.  
Yea thats it my script url:  
[https://is.gd/vxlwvo](https://is.gd/vxlwvo)  
😃 Yea. that’s it.  
Great day!

---

<div class="post-metadata">

**Author:** ![chris](https://talk.code3dgames.com/user_avatar/talk.code3dgames.com/chris/32/5_2.png) [@chris](https://talk.code3dgames.com/u/chris)\
**Post date:** [June 29, 2021, 5:38pm UTC](https://talk.code3dgames.com/t/so-i-actully-thougt-javascript-were-easier-but-nope-it-insnt/302/2 "2021-06-29T17:38:14Z")

</div>

The trouble is in the `sendKeyDown()` function:

```javascript
function sendKeyDown(event) {
  var code = event.code;
  if (code == 'ArrowLeft') marker.position.x = marker.position.x - 5;
  isMovingLeft = true;
  if (code == 'ArrowRight') marker.position.x = marker.position.x + 5;
  isMovingRight = true;
  if (code == 'ArrowUp') marker.position.z = marker.position.z - 5;
  isMovingForward = true;
  if (code == 'ArrowDown') marker.position.z = marker.position.z + 5;
  isMovingBack = true;
}

```

As written, any time a key is pressed (any key – letters, numbers, arrows), this function will

- set `isMovingLeft` to `true`
- set `isMovingRight` to `true`
- set `isMovingForward` to `true`
- set `isMovingBack` to `true`

The `if` statements are only working on the line they are written on. That is, these two lines:

```javascript
  if (code == 'ArrowLeft') marker.position.x = marker.position.x - 5;
  isMovingLeft = true;

```

… say that _if the left arrow is pressed, then subtract 5 from the X position and ALWAYS set `isMovingLeft` to `true`._

What we want to say is _if the left arrow is pressed, then subtract 5 from the X position and if the left arrow is pressed, then set `isMovingLeft` to `true`._

In JavaScript, that is done with “curly braces”:

```javascript
  if (code == 'ArrowLeft') {
    marker.position.x = marker.position.x - 5;
    isMovingLeft = true;
  }

```

That says, _if the left arrow is pressed, then run the two lines of code inside the curly braces (subtract 5 from the X position and set `isMovingLeft` to `true`)._

After adding curly braces around all of the `if` statements in `sendKeyDown()`, that function will look like this:

```javascript
function sendKeyDown(event) {
  var code = event.code;
  if (code == 'ArrowLeft') {
    marker.position.x = marker.position.x - 5;
    isMovingLeft = true;
  }
  if (code == 'ArrowRight') {
    marker.position.x = marker.position.x + 5;
    isMovingRight = true;
  }
  if (code == 'ArrowUp') {
    marker.position.z = marker.position.z - 5;
    isMovingForward = true;
  }
  if (code == 'ArrowDown') {
    marker.position.z = marker.position.z + 5;
    isMovingBack = true;
  }
  
  if (code == 'KeyC') isCartwheeling = !isCartwheeling;
  if (code == 'KeyF') isFlipping = !isFlipping;
}

```

And then it will work as expected. EXCEPT for pressing the down arrow. There’s a bug in the `sendKeyUp()` function that’s preventing that from working. I’m betting you can find that problem, though, so I won’t tell. For now – ask if you can’t track it down 🙂

-Chris

---

<div class="post-metadata">

**Author:** ![Martin](https://talk.code3dgames.com/user_avatar/talk.code3dgames.com/martin/32/41_2.png) [@Martin](https://talk.code3dgames.com/u/Martin)\
**Post date:** [June 30, 2021, 5:40pm UTC](https://talk.code3dgames.com/t/so-i-actully-thougt-javascript-were-easier-but-nope-it-insnt/302/3 "2021-06-30T17:40:03Z")

</div>

I found it, at the line 86 was KeyDown instead of ArrowDown.  
I finally found it, i spent like 1 Hour on it Debugging it.

---

<div class="post-metadata">

**Author:** ![Martin](https://talk.code3dgames.com/user_avatar/talk.code3dgames.com/martin/32/41_2.png) [@Martin](https://talk.code3dgames.com/u/Martin)\
**Post date:** [June 30, 2021, 5:40pm UTC](https://talk.code3dgames.com/t/so-i-actully-thougt-javascript-were-easier-but-nope-it-insnt/302/4 "2021-06-30T17:40:05Z")

</div>

Yea i hope i will find it, bc. i am not so good at debugging

---

<div class="post-metadata">

**Author:** ![chris](https://talk.code3dgames.com/user_avatar/talk.code3dgames.com/chris/32/5_2.png) [@chris](https://talk.code3dgames.com/u/chris)\
**Post date:** [June 30, 2021, 5:42pm UTC](https://talk.code3dgames.com/t/so-i-actully-thougt-javascript-were-easier-but-nope-it-insnt/302/5 "2021-06-30T17:42:51Z")

</div>

Yay! Small mistakes like that are such a pain sometimes. I still do things like that all the time – and it sometimes takes me an hour to track down too!

But nice work tracking it down on your own – it’s good practice, and it eventually gets easier 🙂

-Chris
