Do / While Loops


#1

To the group, I am trying to figure out a way for the user to pick a sword, bow, or maul before slaying the dragon. I would like to check the answer to ensure the user actually picked the choices avaliable. I think you would do this logic test by using a do/while loop i.e., continue prompting the user to pick the correct choice until you got the answer you are looking for. But I am stuck. I can't seem to find examples on how to check the prompt answers for strings. Could someone please help me as I don't know how to resolve this.


var slaying = true
var youHit = Math.floor(Math.random() * 2)
var damageThisRound = Math.floor(Math.random() * 5 + 1)
var totalDamage = 0

var condition = false

do {    
var weaponName = prompt("Choose your destiny to slay this beast. The past has lead our warriors to choose between three schools of weaponry: broadsword, bow, or maul");

if (weaponName != broadsword; bow; maul){
    console.log("Please choose the one of the three weapons");
    condition = false;
}

else {
    console.log("You have chosen wisely");
    condition = true;
}

}
while(weaponName != broadsword; bow; maul);


#2

This could be a long OR expression, or we could rewrite it as a switch. Have you taken switch(), yet?

At any length, we cannot write a conditional with a list, it needs an expression for each operand.

if ( x === 'A" or x === 'B' or x === 'C') {}

The same applies the while conditional expression.


#3

mtf,

To answer your question I have not learned about switches yet.

I appreciate your feedback. I did not know that if statements had that property associated with them (no condition associated with a list).

With that said, it sounds like my best strategy is to control the input. So the user cannot select anything they want.


#4

Remember to write quotes around strings.

Ultimately this is best. When user input is asked for, don’t let them out of the input cycle until they have a valid entry. There are a number of strategies you can use, one of which could be a function that takes a prompt phrase and a list of allowed inputs. It would continue to prompt the user until one of the allowed inputs is received and return that input to the main program.

function user_input(args) {
    var user, i, flag = false, again = "";
    do {
        user = prompt(again + args[0] + ":\n" + args.slice(1).join(', '));
        for (i = 1; i < args.length; i++) {
            if (user === args[i]) return user;
        }
        again = "Try again... ";
    } while ( ! flag && user !== null );
    console.log('Bye');
}

console.log(user_input(["Enter a weapon", "broadsword", "bow", "maul"]));

There is a bundle of concepts in the above that you may not have covered yet. I’ve tried to keep it as simple as I could. I’ll go everything briefly:

First off, notice that the argument for user_input() is an array? The first element is the prompt phrase, the remaining elements are accepted inputs. Having it as a single array permits any number of accepted inputs without having multiple arguments and only a single parameter for the function. The array is commonly known as an arguments list.

It’s quite possible you have not studied loops, yet, and very likely you have not studied array slicing and joining. Quite possibly you have not yet studied functions, either which means this approach is way over the top. Might be a good idea to build the minimal allowed for now to pass this unit, and come back later to improve it when you have a bigger JS toolkit to work from. We should never use code we do not understand so it makes no sense to continue this discussion at the present time. We can pick it up later, when you return. And, we can cover the switch example at that time, too.

Try out the demo

Code
// sample user_input function by Roy

function user_input(args) {
    var user, i, flag = false, again = "";
    do {
        user = prompt(again + args[0] + ":\n" + args.slice(1).join(', '));
        for (i = 1; i < args.length; i++) {
            if (user === args[i]) return user;
        }
        again = "Try again... ";
    } while ( ! flag && user !== null );
    console.log('Bye');
}

console.log(user_input(["Enter a weapon", "broadsword", "bow", "maul"]));

to see how the above user_input function works.


#5

mtf,

I appreciate the code and the couching.

To answer your question insofar I have passed the portion of the course(s) which discuss arrays, for, do, and while loops, and i just finished switches.

You are correct that I have not studied array slicing and I agree this approach is way over the top for me at the moment. I would love to come back to this code to understand it in the future.

I will say that after going of switches that made sense to me in regards to prompting the user on picking a weapon then using default command as a logic test.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.