Level: Leave it to Cleaver

// This shows how to define a function called cleaveWhenClose
// The function defines a parameter called `target`
function cleaveWhenClose(target) 
    if (hero.distanceTo(target) < 5) 
        // Put your attack code here
        // If cleave is ready, then cleave target
    if (hero.isReady("cleave")) {
hero.cleave(target); 
}
        // else, just attack `target`!
    else {
        hero.attack(target); 
}
// This code is not part of the function.
while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        // Note that inside cleaveWhenClose, we refer to the `enemy` as `target`.
        cleaveWhenClose(enemy);
    }
}

Added your suggestions and now it comes up with:
image

you need a bracket

2000000000

function (cleaveWhenClose(target) 
    if (hero.distanceTo(target) < 5) 
        // Put your attack code here
        // If cleave is ready, then cleave target
    if (hero.isReady("cleave")) {
hero.cleave(target); 
}

image

another bracket. needs to be after the second if statement

still not a bracket. In javascript, literally everything needs a bracket.

function (cleaveWhenClose)(target) 
    if (hero.distanceTo(target) < 5))
        // Put your attack code here
        // If cleave is ready, then cleave target
    if (hero.isReady("cleave")) {
hero.cleave(target); 
}

image

function cleaveWhenClose(target) {
// rest of your code
} // end bracket
function cleaveWhenClose(target)
    if (hero.distanceTo(target) < 5))
        // Put your attack code here
        // If cleave is ready, then cleave target
    if (hero.isReady("cleave")) {
    hero.cleave(target); 
    }
}

you need a bracket after the function

function cleaveWhenClose(target) {
function cleaveWhenClose(target) {
    if (hero.distanceTo(target) < 5))
        // Put your attack code here
        // If cleave is ready, then cleave target
    if (hero.isReady("cleave")) {
    hero.cleave(target); 
    }
}

image

if (hero.distanceTo(target) < 5) {
if (hero.isReady("cleave")) {
hero.cleave(target);
}
  }

u should put if (hero.isReady("cleave") && hero.distanceTo(target) < 5) {
and if (target) { cleave else attack

1 Like

oop. yeah. dont do codecombat anymore.

I also managed to finish the level. For me I had to add a ready variable in the function and a hero.attack outside the function

Uh, this was 6 months ago, also, this counts as a solution, please don’t post solutions as this is a learning place :upside_down_face:

2 Likes