4:9 Is this perfectly fine?


#1


Ok well this code is working perfectly fine. Just wanted to know if my solution for when people put inappropriate answers (qn1) is something good, and i want to know how to get them to re-enter their input so that the whole code can re-run and give an answer relevant to the game.


var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
    if (computerChoice < 0.34) 
    {
	    computerChoice = "rock";
} 
else if(computerChoice <= 0.67) 
{
	computerChoice = "paper";
} 
else 
{
	computerChoice = "scissors";
} 
console.log("You: " + userChoice);

console.log("Computer: " + computerChoice);

var compare=function(choice1, choice2){
    if (choice1===choice2)
        return "The result is a tie!"
    else if (choice1==="rock")
         if (choice2==="scissors")
            return "Rock wins"
         else 
            return "Paper wins"
    else if (choice1==="paper")
         if (choice2==="rock")
            return "Paper wins"
        else
            return "Scissors wins"
    else if (choice1==="scissors")
        if (choice2==="paper")
            return "Scissors wins"
        else
            return "Rock wins"
    else
        return "Something must've gone wrong"
    
}

compare(userChoice, computerChoice)


#2

The best way to ensure people are entering an appropriate response or make them re-enter a valid response is to create a recursive function.

/**
*  Getting the valid user input.
**/
function userInput() {
    //ask the question, take in the answer
    var str = prompt("Do you choose rock, paper or scissors?");
   //test if the answer is one of three valid choices
    if(str==="rock" || str==="paper" || str==="scissors")
    {   return str; //return the answer if it is
        }
   else
   {     alert("Please enter a valid response.");
         userInput(); //ask the question and verify user input again if it isn't
       }
}


/**
*  Calling the function.
**/
var userChoice = userInput();

#3

oh ok, but which lesson is this under?


#4

Superlative language is confusing to new learners, and best avoided. What's more, this early in the course track it is not practical or helpful to introduce advanced topics. We need to keep things in perspective.

The member is probably anxious to play a real game, but given that this project appears so early in the track, many of the concepts needed have not yet been introduced. I refer you to the member's last question.


#5

@netpro30633, you can find more about it here:
https://www.codecademy.com/courses/javascript-lesson-205/0/1

If you can grasp the concept of naming a function and calling a function, the idea of a function calling itself shouldn't be too hard. If you prefer not to deal with that, you can do the following with your code (you basically add the rock AND paper AND scissor to all your if statemen ):

var compare=function(choice1, choice2){

//check if userChoice is rock, paper, scissors
    if ( (choice1==="rock" || choice1==="scissors" || choice1 ==="paper") && (choice1===choice2) )
        return "The result is a tie!"
    //rest of if statements omitted
   //[...]
    else
    {
        return "Something must've gone wrong. Did you enter rock, paper, or scissors? Please try again."
 
    }


If I'm confusing you, please let me know. I'm a learner here at Codecademy, too, and I'd hate to be more harmful than helpful. After reading what @mtf said, I didn't really realize that the topic was considered advanced, since it didn't seem advanced to me.

I actually had to find the answers to my questions by searching the depths of the Internet, as I do with most things before writing out a thread topic. The same is true for language, I didn't know what "superlative" meant, as English is my second language, and I went on searching for the meaning of that... :blush: I'm a search maniac.

I really do hope I helped more than harmed...I saw your question wasn't addressed for 6 days, and the thread would close. I wanted to give you the handiest solution I could possibly give :disappointed: But since I can't undo it, I'll answer anything else about it the best I can.


#6

The reason this question went unanswered is because it is not a question, really. What can we answer? Yes? No? Maybe?

Our best explanations are lost on many learners at this level, as accommodating as they may be. There is just not enough under their belt at this point. They are grasping at straws and anything new we introduce opens up a whole new can of worms to have to explain, or teach, as it were. It is untenable in a one-on-one situation. Just count the thousands of answers in this forum. Still the same questions keep coming up.

We try to have learners focus specifically on the courseware lessons and instructions for good reason. It will elevate them up the learning curve. Ever so slightly, albeit, but they are able to move along. Baby steps, maybe; teensie bites is what they have the capacity to consume. We cannot pile stuff on. That never helps.


#7

You are. May as well know that. They have a long way to go before even understanding a function, let alone a recusive one.


#8

Excellent explanation of the situation at hand. As someone a lot more experienced than I am, you would know best at that. Unfortunately, I've already posted what I posted and it cannot be undone, though I don't see how the second bit of code is anything conceptually difficult (adding on more conditionals on if statements).

You've already spoken on the original poster's behalf, and I have stated on that same post that I'm willing to offer help simply because I ended up introducing the topic. If one completes Unit 2 - functions and decide one wants to add one's own spin to it, I assume one understands the basic concept of functions.

From browsing around the forums, I have learned from other posters and also have seen a wide range of learners here too. I have already acknowledged your good input, but you are just sounding like a broken record at this point.

You are offering me a lot of guidance, which I have applied to all other threads I've seen after this one, but none to him, especially given your responses seem to be on his best interest. You should focus more on that, or close the topic if it is not appropriate for these discussion forums.


#9

As forums go, this one is quite unique because each course module has its own Q&A. There really is no way to promote free discussion except in the Corner Bar, else it makes the Q&A confusing and distracts from the intended role.

We also don't solicit other members to take up subjects privately by offering to help. Again, at this stage in people's learning the simpler we can keep things for them, the better. They need to get up to speed in their own time and focus on the course material at hand.

You are welcome to introduce any concepts you wish in the Corner Bar, just not in the tracks. Best if we just coach learners along and let them progress to completion.


#10

hahahahhaha nice to see so many helpful people here, what a nice community :smiley:

ok back to topic, well first, i get the whole idea myssandria is giving me here. The only thing i wanna know is where is the part in which i learn adding more conditions to the if statement( since so far i had 0 idea you could actually add several conditions)

The following is meant for mtf:
maybe its true I don't really understand a function, but i get how to use it. So can you elaborate, or if you want, point me to somewhere in which i can understand functions more in depth?


#11

I dont know whether you are "tracking" this thread, so im just replying to get your attention to this thread :stuck_out_tongue: And really, thank you for your replies, i understand your code, just wanted to know where it can be learned from.

Edit:
Ok your second reply, the longest one. I don't get your code, the "if" condition seems complicated, if possible explain?

I'm curious, i just learnt the "switch" command can we use it?


#12

Er the question is in the reply i made after your last one. And thanks :smiley:


#13

This is exactly what I mean. The concept of logical operators has not been covered in the course, yet. Bringing it up now only muddies the waters at this stage for most learners. Keep forging ahead in the tracks and by the time you finish the JS track you will be able to come back to this module and go to town with it. Now is not the best time.


#14

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