-1

I have a varible

var resources = {
            survivors: 1,
            wood: 0,
            stone: 0,
            food: 0,
            water: 0,   
        };

And in another file I want to add 2 to the resources.wood with resources.wood = resources.wood + parseInt(a); But instead of 2, 4, 6, 8 I get 2222. I also tried with resources.wood += parseInt(a); resources.wood += parseInt('2') resources.wood += parseInt(a) resources.wood += parseInt("2") and so on but i just get 22222222 as output.

I am running this code to try and add resources.wood.


setInterval(function(){
    
    
    if(woodChopingWorkers >0){
    
        resources.wood += (woodChopingWorkers*multiplier)/10;
        
        console.log(resources.wood);
    
    }
    
}, 100)

  • 4
    It suggests that `resources.wood` is a string itself, as you're parsing `a` but receive concatenation instead of addition. – VLAZ Jan 13 '21 at 12:34
  • 2
    `resources.wood = parseInt(resources.wood) + parseInt(a);` …? – CBroe Jan 13 '21 at 12:35
  • You can start using Typescript. Typescript is javascript but better. Typescript can help you validate your code with type checking. – Nik Jan 13 '21 at 12:38
  • 1
    Is this really your actual code? Did you accidentally put `wood: '0'` or use `resources.wood = '0'` somewhere? – Jay Buckman Jan 13 '21 at 12:38
  • also you should add a radix to `parseInt` see: https://stackoverflow.com/questions/6611824/why-do-we-need-to-use-radix-parameter-when-calling-parseint – novarx Jan 13 '21 at 12:44
  • Can you add a code snippet of what is the number you are trying to add? – p-syche Jan 13 '21 at 12:51

4 Answers4

0

A possible problem, in this case, should be your initial object. If you are using * or / automatically your string converts to number, but in + concat happens, so double-check your initial state should be changed when you try to change them using input or something!

Try Change initial wood in this example!

var resources = {
    survivors: 1,
    wood: 0,
    stone: 0,
    food: 0,
    water: 0,   
};
const multiplier = 1;

const initWood = () => {
   resources.wood = document.getElementById('myWoodInput').value
}

setInterval(()=>{
  const woodChopingWorkers = document.getElementById('myWorkersInput').value;
  if(woodChopingWorkers > 0){
    resources.wood += (woodChopingWorkers*multiplier)/10;
    console.log(resources.wood);
  }
}, 100)
initial wood : <input id="myWoodInput" type="number" value="0" oninput="initWood()"/>
</br>
number of wood choping workers : <input id="myWorkersInput" type="number" value="2"/>

Solution: use parseInt() to make sure your wood is always a number:

var resources = {
    survivors: 1,
    wood: 0,
    stone: 0,
    food: 0,
    water: 0,   
};
const multiplier = 1;

const initWood = () => {
   resources.wood = parseInt(document.getElementById('myWoodInput').value)
}

setInterval(()=>{
  const woodChopingWorkers = document.getElementById('myWorkersInput').value;
  if(woodChopingWorkers > 0){
    resources.wood += (woodChopingWorkers*multiplier)/10;
    console.log(resources.wood);
  }
}, 100)
initial wood : <input id="myWoodInput" type="number" value="0" oninput="initWood()"/>
</br>
number of wood choping workers : <input id="myWorkersInput" type="number" value="2"/>
b3hr4d
  • 4,012
  • 1
  • 11
  • 24
0

use parseInt to resources.wood as well and dont use += mark on json object it directly update your Json Object

resources.wood = parseInt(resources.wood) + parseInt(a)

And in your function

 setInterval(function(){
        if(woodChopingWorkers >0){
            let localresourcesWood = (woodChopingWorkers*multiplier)/10;
            let resourcesWood = localresourcesWood + parseInt(resources.wood);
            console.log(resourcesWood);
            resources.wood = resourcesWood; //to update json wood object
        }
    }, 100)
Sanoodia
  • 830
  • 4
  • 9
  • When he tries to make a game or something he should probably change the JSON Object to update the game state! – b3hr4d Jan 13 '21 at 13:23
-1

in order to get two numbers to add up they have both to be of type number if one of them is of type string then the other will be casted to a type string and you will end up with a string concatenation like this example

const resources = {
  survivors: 1,
  wood: 0,
  stone: 2,
  food: 0,
  water: "1",
};

const {survivors , wood , stone , food , water} = resources



document.getElementById('sw').innerHTML = `Survivours ${survivors} + Wood ${wood} = ` + (survivors + wood)

document.getElementById('ws').innerHTML = `Wood ${wood} + Stone ${stone} = ` + (wood + stone)

document.getElementById('sf').innerHTML = `Stone ${stone} + Food ${food} = ` + (stone + stone)

document.getElementById('sf').innerHTML = `food ${food} + "string" water ${water} = ` + (food + water) + "<strong> as you can see when we concat a number to a string the number get caster to a string and we have a string concat</strong>" 
<span id='sw'></span>
<hr/>
<span id='ws'></span>
<hr/>
<span id='sf'></span>
<hr/>

<span id='fw'></span>
Chamsddine Bouzaine
  • 1,299
  • 10
  • 17
-1

I manged to find a way to do it.

resources.wood = parseFloat(resources.wood + (woodChopingWorkers*multiplier)/10);

Thanks for the help I just needed to chnge parseInt to parseFloat becuse I have values deciamal places.