Need some final help with this program plz urgent!

javascript

#1

so i finished this code and it seems to work but only for the last user choice
if i input 10 it will work but when i input 9 it wont print the console log
if i remove 10 so 9 is the last imput it will work
what do i need to change? could someone show me plz?

var userChoice = prompt("how are you feeling today on a scale from 1 to 10?");
var computerChoice = Math.random();
if (userChoice == 1) {
if (computerChoice <= 0.33){
computerChoice = "aww that sucks! hope you feel better soon";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "thats not nice to hear :frowning: hope you will cheer up soon!";
}
else{
computerChoice = "thats afwull! hope you have a nice day anyway!";
}
}

var computerChoice = Math.random();
if (userChoice == 2) {
if (computerChoice <= 0.33){
computerChoice = "aww that sucks! hope you feel better soon1";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "thats not nice to hear :frowning: hope you will cheer up soon1!";
}
else{
computerChoice = "thats afwull! hope you have a nice day anyway1!";
}
}

var computerChoice = Math.random();
if (userChoice == 2) {
if (computerChoice <= 0.33){
computerChoice = "ooh thats not good! heres a smiley for you! :)";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "to bad! cheer up it will get better!";
}
else{
computerChoice = "aww that terrible :frowning: thinks will get beter soon!";
}
}

var computerChoice = Math.random();
if (userChoice == 3) {
if (computerChoice <= 0.33){
computerChoice = "just hang in there things will get better";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "im here for you :slight_smile: to cheer you up with a smiley :)";
}
else{
computerChoice = "aww you can do better! cheer up";
}
}

var computerChoice = Math.random();
if (userChoice == 4) {
if (computerChoice <= 0.33){
computerChoice = "thats not so good. but hey thinks will get better soon :)";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "hmm thats not so nice to hear, cheer up champ!";
}
else{
computerChoice = "there are allot off people out there! your not alone";
}
}

var computerChoice = Math.random();
if (userChoice == 5) {
if (computerChoice <= 0.33){
computerChoice = "mwhaa could be better right :wink:";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "comon cheer up mate! your not alone!";
}
else{
computerChoice = "hang in there champ! thinks will get better";
}
}

var computerChoice = Math.random();
if (userChoice == 6) {
if (computerChoice <= 0.33){
computerChoice = "me too.. me too..";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "hey! thats not so bad, your day might get even better!";
}
else{
computerChoice = "hey thats not bad at all there are people out there doing worse!";
}
}

var computerChoice = Math.random();
if (userChoice == 7) {
if (computerChoice <= 0.33){
computerChoice = "nice! keep up the good vibes";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "hey you! you rock!";
}
else{
computerChoice = "keep it up! you might cheer up someone else with your good atitude :)";
}
}

var computerChoice = Math.random();
if (userChoice == 8) {
if (computerChoice <= 0.33){
computerChoice = "marvelous! keep it up!";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "thats so great to hear!";
}
else{
computerChoice = "ooh wauw! thats great to hear!";
}
}

var computerChoice = Math.random();
if (userChoice == 9) {
if (computerChoice <= 0.33){
computerChoice = "yeaah! great to see ur happy!";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "wonderfull! keep up the good vibes!";
}
else{
computerChoice = "hey there! did you know?.. you are awsome!";
}
}

var computerChoice = Math.random();
if (userChoice == 10) {
if (computerChoice <= 0.33){
computerChoice = "did you just win the lottery??";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "amazing! i hope you keep feeling this way!";
}
else{
computerChoice = "great! share your good vibes!";
}
}


Need some help
#2

you already got the solution to this problem in your other topic, prompt will store a string:

var userChoice = prompt("how are you feeling today on a scale from 1 to 10?");
console.log(typeof(userChoice));

comparing string numbers with integers is not nice to do, cast userChoice to a integer.


#3

i dont quite understand what you mean srry im a javascript noob
could you please correct my code so i can see what you did?

var userChoice = prompt("how are you feeling today on a scale from 1 to 10?");
var computerChoice = Math.random();
if (userChoice == 1) {
if (computerChoice <= 0.33){
computerChoice = "aww that sucks! hope you feel better soon";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "thats not nice to hear :frowning: hope you will cheer up soon!";
}
else{
computerChoice = "thats afwull! hope you have a nice day anyway!";
}
}

var computerChoice = Math.random();
if (userChoice == 2) {
if (computerChoice <= 0.33){
computerChoice = "aww that sucks! hope you feel better soon1";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "thats not nice to hear :frowning: hope you will cheer up soon1!";
}
else{
computerChoice = "thats afwull! hope you have a nice day anyway1!";
}
}

var computerChoice = Math.random();
if (userChoice == 2) {
if (computerChoice <= 0.33){
computerChoice = "ooh thats not good! heres a smiley for you! :)";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "to bad! cheer up it will get better!";
}
else{
computerChoice = "aww that terrible :frowning: thinks will get beter soon!";
}
}

var computerChoice = Math.random();
if (userChoice == 3) {
if (computerChoice <= 0.33){
computerChoice = "just hang in there things will get better";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "im here for you :slight_smile: to cheer you up with a smiley :)";
}
else{
computerChoice = "aww you can do better! cheer up";
}
}

var computerChoice = Math.random();
if (userChoice == 4) {
if (computerChoice <= 0.33){
computerChoice = "thats not so good. but hey thinks will get better soon :)";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "hmm thats not so nice to hear, cheer up champ!";
}
else{
computerChoice = "there are allot off people out there! your not alone";
}
}

var computerChoice = Math.random();
if (userChoice == 5) {
if (computerChoice <= 0.33){
computerChoice = "mwhaa could be better right :wink:";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "comon cheer up mate! your not alone!";
}
else{
computerChoice = "hang in there champ! thinks will get better";
}
}

var computerChoice = Math.random();
if (userChoice == 6) {
if (computerChoice <= 0.33){
computerChoice = "me too.. me too..";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "hey! thats not so bad, your day might get even better!";
}
else{
computerChoice = "hey thats not bad at all there are people out there doing worse!";
}
}

var computerChoice = Math.random();
if (userChoice == 7) {
if (computerChoice <= 0.33){
computerChoice = "nice! keep up the good vibes";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "hey you! you rock!";
}
else{
computerChoice = "keep it up! you might cheer up someone else with your good atitude :)";
}
}

var computerChoice = Math.random();
if (userChoice == 8) {
if (computerChoice <= 0.33){
computerChoice = "marvelous! keep it up!";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "thats so great to hear!";
}
else{
computerChoice = "ooh wauw! thats great to hear!";
}
}

var computerChoice = Math.random();
if (userChoice == 9) {
if (computerChoice <= 0.33){
computerChoice = "yeaah! great to see ur happy!";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "wonderfull! keep up the good vibes!";
}
else{
computerChoice = "hey there! did you know?.. you are awsome!";
}
}

var computerChoice = Math.random();
if (userChoice == 10) {
if (computerChoice <= 0.33){
computerChoice = "did you just win the lottery??";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "amazing! i hope you keep feeling this way!";
}
else{
computerChoice = "great! share your good vibes!";
}
}


#4

well, if you don't know what strings are, and what integers are i highly recommend you to read on some documentation about javascript data types. (okay, in javascript integers seemed to be called numbers):

var foo = 42; // foo is now a Number
var foo = "bar"; // foo is now a String
var foo = true; // foo is now a Boolean

Did you run the code i gave you?

anyway, userChoice contains a string, which you are going to compare to a integer:

if ("10" == 10)

userChoice ("10") is a string, google how to convert strings to integers in javascript, since you will need to convert userChoice into integer


#5

If you're the type who forgets to put the radix in when you call parseInt, you can use parseFloat and round it however you like. Here I use floor.

var floor = Math.floor;
var x = floor(parseFloat("1000.01"));

If you use this it should work :slight_smile:


#6

You can go to stackoverflow and find out how to convert it and etc.. :runner:


#7

where do i paste this in?


#8

well either after the prompt or combine it directly with the prompt


#9

could you paste it in my code it doesnt work


#10

i wouldn't use that code, use parseInt, see mdn documentation on how to use it:


#11

sorry i dont understand this at all i just started learing js


#12

Ok. So just trying to get this all straight. You want to know in your current code back in this reply

why inputting 10 will work but 9 will not in your current code. However if 10 is removed it works. First, did this pass the lesson for you? Second, are you wanting an explanation of why this works or doesn't work? Third, if you did not pass the lesson then what error message are you getting so we can determine what needs to be fixed to get your code working to pass the lesson. Sorry if you have gotten confused. I just started learning Javascript a couple of months ago and I realize that stuff like this can be overwhelming especially in an extremely long set of code. If need be, I can work through the lesson with your code for you to determine what you want to know I just need to know what exactly you are wanting! :smiley:


#13

well i dont quite understand what or were to imput the solutions you guys give. so if you could jsut edit my code to make it work and then i can have my teacher explain it to me wich is allot beter for me
if you would do this for me that would be verry nice


#14

Ok! I believe we can do that! It might take a little bit of time however since I have not started the new Learn Javascript course so it might take some time for me to get to that lesson and try to use your code to pass the lesson and fix it so it does pass the lesson. I might can get someone who has already worked through this particular Learn Javascript session to go back and compare their code to yours and message what you need to fix. Whatever the way, we will help you fix your problem!


#15

thank you verry much thats helps me out alot!


#16

Is there a particular due date or anything on you completing this assignment that I need to be aware of? Also what lesson is this on Learn Javascript? I would like to make a beeline for it so I can get your answer back to you as soon as possible.


#17

i must have it finished by thursday and its the assignment is to create a simple program in js that uses if else and math random inspired by the rock paper siccor assignemt

if this so far but it only works if the user inputs 10 as answer in the prompt and i dont know how to fix it
heres the code:

var userChoice = prompt("how are you feeling today on a scale from 1 to 10?");
var computerChoice = Math.random();
if (userChoice == 1) {
if (computerChoice <= 0.33){
computerChoice = "aww that sucks! hope you feel better soon";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "thats not nice to hear :frowning: hope you will cheer up soon!";
}
else{
computerChoice = "thats afwull! hope you have a nice day anyway!";
}
}

var computerChoice = Math.random();
if (userChoice == 2) {
if (computerChoice <= 0.33){
computerChoice = "ooh thats not good! heres a smiley for you! :)";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "to bad! cheer up it will get better!";
}
else{
computerChoice = "aww that terrible :frowning: thinks will get beter soon!";
}
}

var computerChoice = Math.random();
if (userChoice == 3) {
if (computerChoice <= 0.33){
computerChoice = "just hang in there things will get better";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "im here for you :slight_smile: to cheer you up with a smiley :)";
}
else{
computerChoice = "aww you can do better! cheer up";
}
}

var computerChoice = Math.random();
if (userChoice == 4) {
if (computerChoice <= 0.33){
computerChoice = "thats not so good. but hey thinks will get better soon :)";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "hmm thats not so nice to hear, cheer up champ!";
}
else{
computerChoice = "there are allot off people out there! your not alone";
}
}

var computerChoice = Math.random();
if (userChoice == 5) {
if (computerChoice <= 0.33){
computerChoice = "mwhaa could be better right :wink:";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "comon cheer up mate! your not alone!";
}
else{
computerChoice = "hang in there champ! thinks will get better";
}
}

var computerChoice = Math.random();
if (userChoice == 6) {
if (computerChoice <= 0.33){
computerChoice = "me too.. me too..";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "hey! thats not so bad, your day might get even better!";
}
else{
computerChoice = "hey thats not bad at all there are people out there doing worse!";
}
}

var computerChoice = Math.random();
if (userChoice == 7) {
if (computerChoice <= 0.33){
computerChoice = "nice! keep up the good vibes";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "hey you! you rock!";
}
else{
computerChoice = "keep it up! you might cheer up someone else with your good atitude :)";
}
}

var computerChoice = Math.random();
if (userChoice == 8) {
if (computerChoice <= 0.33){
computerChoice = "marvelous! keep it up!";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "thats so great to hear!";
}
else{
computerChoice = "ooh wauw! thats great to hear!";
}
}

var computerChoice = Math.random();
if (userChoice == 9) {
if (computerChoice <= 0.33){
computerChoice = "yeaah! great to see ur happy!";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "wonderfull! keep up the good vibes!";
}
else{
computerChoice = "hey there! did you know?.. you are awsome!";
}
}

var computerChoice = Math.random();
if (userChoice == 10) {
if (computerChoice <= 0.33){
computerChoice = "did you just win the lottery??";
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = "amazing! i hope you keep feeling this way!";
}
else{
computerChoice = "great! share your good vibes!";
}
}


#18

Hello what I mean is to concatenate you need parseFloat , I gave an example which won't work with your code but it's just the bottom line :wink:


#19

Could you please tell me what the error message is coming up as and give me the link to the exercise please :wink:


#20

it doesnt give an error it just only prints the console log if u imput 10 in the prompt

its not a codecedemy assingment but its inspired by the rock paper siccor assingment