Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript HELP!

WishICouldCode

New Coder
Im not sure what I did wrong, but the Wood amount in the Campain tab and the Follower Cost goes NaN when clicked. HELP!


HTML:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- All the Crazy CSS -->
<style>
body {font-family: Arial;}

/* Style the tab */
.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  background-color: #f1f1f1;
}

/* Style the buttons inside the tab */
.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.3s;
  font-size: 17px;
}

/* Change background color of buttons on hover */
.tab button:hover {
  background-color: #ddd;
}

/* Create an active/current tablink class */
.tab button.active {
  background-color: #ccc;
}

/* Style the tab content */
.tabcontent {
  display: none;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}
</style>
</head>


<body>

<h2>Sirrulith</h2>
<p>For years mosters have roamed the land.</p>
<p>You seek to change that...</P>

<div class="tab">
  <button class="tablinks" onclick="openCity(event, 'Campaign')">Campaign</button>
  <button class="tablinks" onclick="openCity(event, 'Camp')">Camp</button>
  <button class="tablinks" onclick="openCity(event, 'Empowerments')">Empowerments</button>
</div>

<div id="Campaign" class="tabcontent">
  <h3>Campaign</h3>
  <div class="clickToGain">
        <div class="soulThings">   
          <button onclick="soulsClick(1)" >Purge</button>
          <br />
          Souls: <span id="souls">0</span>
        </div>
        <div class="woodThings">
          <button onclick="woodClick(1)">Cut Tree</button>
          <br />
          Wood: <span id="wood">0</span>
        </div>
      </div>
      <div class="buyables"> 
        <div class="followerThings">
            <button onclick="buyFollower()">Inlist</button>
            <br />
            Followers: <span id="follower">0</span>
            <br />
            Follower Cost: <span id="followerCost">10</span> souls
            <br />
        </div>
</div>
    <div class="campThings">
        <button onclick="buyCamps()">Camp</button>
        <br />
        Camp: <span id="camps">0</span>
        <br/>
        Camp Cost: <span id="campsCost">15</span> wood
    </div>
  </div>
</div>
</div>
 
<div id="Camp" class="tabcontent">
  <h3>Camp</h3>
</div>

<div id="Empowerments" class="tabcontent">
  <h3>Empowerments</h3>
  <p>Tokyo is the capital of Japan.</p>
</div>

<script>
// Hello! If you are here, I hope you aren't cheating! :D


// All the resource stuff
var souls = 0;
checkSouls()
var wood = 0;
checkWood()

var soulsAllNum = 0;

function soulsClick(number){
    souls = souls + number;
    document.getElementById("souls").innerHTML = souls;
    checkSouls()
};

function woodClick(number){
    wood = wood + number;
    document.getElementById("wood").innerHTML = wood;
    checkWood()
};


// Stuff you can buy
var follower = 0;

function buyFollower(){
    var followerCost = Math.floor(10 * Math.pow(1.50,follower));     //works out the cost of this follower
    if(souls >= followerCost){                                   //checks that the player can afford the follower
        follower = follower + 1;                                   //increases number of followers
        souls = souls - followerCost;                          //removes the souls spent
        document.getElementById('follower').innerHTML = follower;  //updates the number of followers for the user
        document.getElementById('souls').innerHTML = souls;
    };
    var nextCostFollowers = Math.floor(10 * Math.pow(1.5,follower));       //works out the cost of the next follower
    document.getElementById('followerCost').innerHTML = nextCostFollowers;  //updates the follower cost for the user
    checkSouls()
};


var camps = 0;

function buyCamps(){
    var campsCost = Math.floor(10 * Math.pow(1.1,camps));
    if(wood >= campsCost){                                   
        camps = camps + 1;
        wood = wood - campsCost;                         
        document.getElementById('camps').innerHTML = camps; 
        document.getElementById('wood').innerHTML = wood;
  
    };
    var nextCostCamps = Math.floor(10 * Math.pow(1.5,camps));       //works out the cost of the next follower
    document.getElementById('campsCost').innerHTML = nextCostCamps;  //updates the follower cost for the user
};

function checkSouls(){
  if (souls <= 10){
    document.getElementById("followerBuyButton").disabled = true;
  } else {
    document.getElementById("followerBuyButton").disabled = false;
  };
}

function checkWood(){
  if (wood <= 15){
    document.getElementById("campBuyButton").disabled = true;
  } else {
    document.getElementById("campBuyButton").disabled = false;
  };
}

// Tick settings
window.setInterval(function(){
    soulsClick(follower);
}, 1500);


//Tab Stuff
function openCity(evt, cityName) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(cityName).style.display = "block";
  evt.currentTarget.className += " active";
}
</script>
  
</body>
</html>
 
Hello, @WishICouldCode welcome to Code!

First things first, please create a thread title that can be easily understood and has some value. This can be useful for those who are trying to find a solution to your question. Review the following from the Code Of Conduct:
Inappropriate Content:
  1. When posting you need to be sure to add some value to your post. Add lots of information, explain yourself, make your post enjoyable to read.
    1. This shall include thread titles, or titles or resources, media items.

Now, I'm not too familiar with JavaScript. However, after reviewing your code I noticed that you are declaring some of your variables throughout the script. But, remember a computer reads the code line by line so when you declare a variable it should be relatively near the top so the computer now understands them, then call your functions.

Try declaring all your variables at the top of the script and then follow by calling the functions. Let me know if that helped!
 
Back
Top Bottom