0

This is literally the first thing I have ever coded and I am self-taught, so I'm sure it's a mess and I've made a dumb error, but after an hour of looking up the issue and trying things I cannot solve this.

To try and teach myself very basic javascript concepts I've written a textbot that has existential conversations with you. The problem is that regardless of user input, all the if statements for the condition of whatDream are printing in a row. Can anyone tell me what I've screwed up here? Apologies if my methods are wack - 100% of what I'm doing is from self teaching and letting myself flail to learn, and I'll appreciate any advice.

function yesHappy (){
     alert ("Good for you. To be happy as a human is a great gift.");
     alert ("The content human starts over again everyday, in spite of all they know, against all they know.");
     alert ("If one advances each day confidently in the direction of their dreams, and endeavors to live the life which they have imagined, they will meet with a success unexpected in common hours.");
     var g = 0
     while (g < 2) {
      g = 0
      var whatDream = prompt ("What is your dream?");
      if (whatDream === "success" || "Success" || "power" || "Power" || "wealth" || "Wealth"){
       alert ("That is a goal that takes much will in the earthly domain, and is not one I can readily reccomend.");
       alert ("But, read your fate, see what is before you, and walk into futurity.");
       g = 3
      }
      if (whatDream === "friends" || "Friends" || "family" || "Family" || "love" || "Love"){
       alert ("To surround oneself with others fulfills a basic human goal to be social, thus your dream is only natural.");
       alert ("Properly speaking, a human has as many social selves as there are individuals who recognize them.");
       alert ("To all of these you surround yourself with, see in your goals only to show your true social self.");
       g = 3
      }
      if (whatDream === "fun" || "Fun" || "charity" || "Charity" || "faith" || "Faith" || "travel" || "Travel"){
       alert ("These are honorable dreams, chosen to bring joy to yourself and others.");
       alert ("Above all however, avoid falsehood, especially falseness to yourself.");
       alert ("Be true to your purpose.");
       g = 3
      }
      if (g === 0){
       alert ("Though knowledge is the plague of life, and consciousness an open wound in its heart, I am still sad to say I don't know what you mean.");
       alert ("Let's try that again.");
      }
     }
    }
qiAlex
  • 4,290
  • 2
  • 19
  • 35
  • 2
    `thing === a || b` is not valid syntax in javascript. you cannot chain conditionals like this – Taplar Nov 01 '18 at 20:43
  • You can do logic like this with a list check. For instance `['success', 'Success', 'power', 'Power'].indexOf(whatDream) > -1` – Taplar Nov 01 '18 at 20:44
  • Check the difference between `(whatDream === "success" || "Success")` and `(whatDream === "success" ||whatDream === "Success")` – Maksim Nov 01 '18 at 20:46
  • Hi, nice syntax PtitsaDroog but as said Taplar the correct syntax is rather: `if (whatDream === "success" || whatDream === "Success" || whatDream === "power" || whatDream === "Power" || whatDream === "wealth" || whatDream === "Wealth")` – Ermac Nov 01 '18 at 20:49
  • I would argue the array check is nicer to read. It's also a singular operation, vs multiple with chained conditionals – Taplar Nov 01 '18 at 20:52
  • I have updated your code here https://www.w3schools.com/code/tryit.asp?filename=FWT9LLI219PT you can go to this link and click run to check results the only issue with your code is the same as @ankabout has mentioned. – Zain Aftab Nov 01 '18 at 20:52
  • @Taplar Your code is better indeed but in my opinion for a beginner he can use the multiple `if` conditions =) – Ermac Nov 01 '18 at 20:54
  • The list check doesn't reduce the number of if statements. It reduces the numer of || conditionals in the ifs. @ankabout – Taplar Nov 01 '18 at 20:55
  • side note: If you're not worried about case, you can save effort by doing something like `whatDream = whatDream.toLowerCase()` after your prompt. Then you wouldn't have to worry about separate checks for "success" and "Success" etc. – David784 Nov 01 '18 at 21:04

1 Answers1

0

You need to refactor all your conditional statements. For example: This one

if (whatDream === "success" || "Success" || "power" || "Power" || "wealth" || "Wealth")

Would have to be translated to:

if (whatDream === "success" || whatDream === "Success" || whatDream === "power" || whatDream === "Power" || whatDream === "wealth" || whatDream === "Wealth")

Which does what you want: Checks to see if whatDream equals any of those. Or alternatively you can use a data structure and use a method to check if the string exists in that data structure- but above is the simplest refactoring you'd have to do.

Javascript has "truthy" and "Falsey" values. Strings like "Success" map to true whereas "" map to false. So your conditionals were always evaluating to true because strings with length > 0 are "truthy". This is probably more explanation that you need- but its important to know that in the future of javascript development.

In essence- your issue is that you weren't checking the values of whatDream in your boolean logic. The above simple refactoring I did fixes it.

If you're curious: You can read up on truthy and falsey values here:

https://j11y.io/javascript/truthy-falsey/

chevybow
  • 9,959
  • 6
  • 24
  • 39