Javascript boolean: Annalyn's infiltration

Hi all
sorry to bother, but I’m trying to complete the boolean exercise, but as I’ve never programmed before, I’m a bit stuck on how to implement boolean logic. Could anyone help? The stub is `export function canExecuteFastAttack(knightIsAwake) {

}`
knightIsAwake is a constant set to true. This function is supposed to implement boolean logic to return false if knightIsAwake is true, otherwise the function returns true.

Hi and welcome!

Let’s start with the logic of the function, without any code involved.

How would you, as a human, determine the result if you were given the value of knightIsAwake?
What is the result of canExecuteFastAttack(true)?
What is the result of canExecuteFastAttack(false)?
Can you describe how you get from the value of the parameter to the result?

I would determine the result by stating the opposite of knightIsAwake. We know that if the knight is awake, the fast attack cannot be used, and if the knight is asleep, the fast attack can be used.

The result of canExecuteFastAttack(true) would be false, and canExecuteFastAttack(false) would be true.

Great!

Do you know how to produce the “opposite” of a boolean?
Do you know any operation where “operation(true)” is false, and “operation(false)” is true?

Would the NOT operator be of use here (!)? Would that be placed within the function to return the opposite value to knightIsAwake?

“NOT” is a good idea.
Now let’s transform that into code.

Do you know the JavaScript syntax of a boolean “NOT”?

I don’t know exactly, but is it as simple as:

export function canExecuteFastAttack(knightIsAwake) {
return !knightIsAwake;
}

That looks pretty good. Go on, try it!

Have done, it worked. Many thanks.

Happy coding!

sorry again, but on the final task, you are asked to complete the function
‘export function canFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent)’
I know a long-winded way of completing this (using an if statement and going through every requirement/possibility and returning true/false) but i was wondering if there was a quicker, simpler way to do this.

No worries.

From the instructions:

  • If Annalyn has her pet dog with her she can rescue the prisoner if the archer is asleep. The knight is scared of the dog and the archer will not have time to get ready before Annalyn and the prisoner can escape.
  • If Annalyn does not have her dog then she and the prisoner must be very sneaky! Annalyn can free the prisoner if the prisoner is awake and the knight and archer are both sleeping, but if the prisoner is sleeping they can’t be rescued: the prisoner would be startled by Annalyn’s sudden appearance and wake up the knight and archer.

How would you write the function canFreePrisoner

  • if only the first of the two conditions must be considered?
  • if only the second of the two conditions must be considered?

Edit: Sorry, I might have misunderstood your question. This function wants you to combine the four parameters. You can do that in a single boolean expression, or with separate if / return statements. The second option might seem simpler at first, but I’d like to encourage you to try writing the function as a single return statement as well, if only as an exercise. Then choose what you find easier to read.

for just the first:

‘export function canFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent) {
if knightIsAwake == false && archerIsAwake == false && prisonerIsAwake == false && petDogIsPresent == false) {
return false;
}’

and for the second:

‘export function canFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent) {
if knightIsAwake == false && archerIsAwake == false && prisonerIsAwake == false && petDogIsPresent == true) {
return true;
}’

Instead of

some_boolean_value == true

you can just write

some_boolean_value

And the JavaScript programmers I know prefer the operator ! instead of comparing a boolean value with == false.

Just seen your edit. Do you mean without an if statement?

Do you mean without an if statement?

Similar to the first function canExecuteFastAttack, instead of

export function canExecuteFastAttack(knightIsAwake) {
  if (!knightIsAwake) {
    return true;
  } else {
    return false;
  }
}

you can write

export function canExecuteFastAttack(knightIsAwake) {
  return !knightIsAwake;
}
1 Like

ah yes. Thank you. I’ll post if I run into any more problems