How to make an element do nothing

Hi I’m making a small game. It involves a vacuum cleaner. I want the vacuum only to move up and down when the space key is pressed.
My problem is the vacuum is moving up and down when any key is pressed, and when more than one key is pressed at the same time the vacuum gets buried somewhere on the page.

window.onload = docReady();

var vacuum = document.getElementById('vacuum');
var spaceKey = 32;
var fired = false;


function setPosition(element, x, y){
    element.style.left = x + 'px';
    element.style.top = y + 'px';
}

function spaceReleased() {
    window.onkeyup = function() {
        vacuum.style.top = parseInt(vacuum.style.top) + 100 + 'px';
        fired = false;
    }   
}

function moveVacuum(evt) {
    if(fired === false) {
        fired = true;
        vacuum.style.top = parseInt(vacuum.style.top) - 100 + 'px';
    }

    if(evt.keyCode !== spaceKey) {
        //do nothing
    }

    console.log(evt.keyCode)
 
}


window.addEventListener('keydown', moveVacuum);
window.addEventListener('keyup', spaceReleased);

function docReady(){
    var vacuum = document.getElementById('vacuum');
    setPosition(vacuum, 550, 415);
    
}

you need to make so that all the movement code execute only for the right key - you need to wrap it all in an if statement

Like this?

function moveVacuum(evt) {
    if(fired === false) {
        fired = true;
        if(evt.keyCode === spaceKey) {
            fired = true;
            vacuum.style.top = parseInt(vacuum.style.top) - 100 + 'px';     
        } else if(evt.keyCode !== spaceKey) { 
            //do nothing
    
        }
    }

I’m still not sure what the vacuum should do if the key pressed is NOT space. That’s why I need to figure out how to make it do nothing.

still looking for a bit of help.

Hey there,

do you have a specific question?

An if statement only runs if the condition is true.
If your code should do nothing, you can remove the else.

if(spaceKeyPressed){
  doThis();
}

doNextStep();

Hi yes sorry if I’m not being clear. My question is how can I have the vacuum move up and down ONLY when the space key is pressed (I don’t want any other keys to trigger this event).

EDIT: Was able to figure it out!