0

I am building this tarot program, but I am having trouble with the Boolean statements. I only want to show when TRUE statements are executed, but for some reasons, this program is also executing FALSE statement, and is giving me a blank page which I don't want. Here is a link of my project https://jsfiddle.net/4vsyxj3g/.

Here is what I am trying to accomplished. The Tarot card has about 72 cards, and one card can be in the past,present, or future, but it cannot be duplicated. For example, one card cannot be in the past, or in the present, or in the future, at the same time. The program must select a unique card for the past, present, and future. Anyways, thank you for all the help.

Here is the code:

  let a = Math.floor(Math.random() * 3)
  let b = Math.floor(Math.random() * 3)
  let c = Math.floor(Math.random() * 3)

let x = true
console.log(a + " " + b + " " + c); 
 


if (x)
{

    if(a != b && a != c && a != c && b != c)

  {



    let randomPastCard = document.getElementById("textNamePast")
    randomPastCard.textContent = card[a].name
    
    let pastTextCard = document.getElementById("pastText")
    pastText.textContent = card[a].past
    
    
    var img = document.createElement("img");
    img.src = card[a].img;
    document.getElementById('pastTarotImg').appendChild(img);
    
    
    
    let randomPresentCard = document.getElementById("textNamePresent")
    randomPresentCard.textContent = card[b].name
    
    
    let presentTextCard = document.getElementById("presentText")
    presentTextCard.textContent = card[b].present
    
    var img = document.createElement("img");
    img.src = card[b].img;
    document.getElementById('presentTarotImg').appendChild(img);
    
    
    
    
    let randomFutureCard = document.getElementById("textNameFuture")
    randomFutureCard.textContent = card[c].name
    
    
    let futureTextCard = document.getElementById("futureText")
    futureTextCard.textContent = card[c].future
    
    var img = document.createElement("img");
    img.src = card[c].img;
    document.getElementById('futureTarotImg').appendChild(img);

  
console.log("true1");

    x = true

  }
<hh class="html">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="textNamePast"></div> 
    <div id="pastText"></div> <br>
    <div id="pastTarotImg"></div>

    
    <div id="textNamePresent"></div> 
    <div id="presentText"></div> <br>
    <div id="presentTarotImg"></div>



    <div id="textNameFuture"></div> 
    <div id="futureText"></div>    <br>
    <div id="futureTarotImg"></div>

    <br><br>

    
    <br><br>
    
    
    
<script src="tarot.js"></script>
</body>
</html>


}

else
{
    console.log("false");
    x = false

}
1
  • You have set let x = true always true. Commented Aug 13, 2021 at 5:36

1 Answer 1

1

Your code is perfectly fine. If you keep reloading your page, it will actually give you a rendered version. The issue here is that if the numbers you got are equal, you don't do anything else.

You can solve this if you have a while loop that essentially runs until you get unique numbers, then uses that.

You can do that by:

while (a === b || a === c || a === c || b === c) {
  let a = Math.floor(Math.random() * 3)
  let b = Math.floor(Math.random() * 3)
  let c = Math.floor(Math.random() * 3)
}

// Then use those values like normal here

This should solve your problem. On the off-side of things, may I ask. Why are you doing this?:

let x = true;
if (x) {
  // all code here
}
Sign up to request clarification or add additional context in comments.

1 Comment

It worked with your code. Here is the link jsfiddle.net/3dy7ano9. I was using let x= true because I though it would work with a Boolean statement, but it didn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.