Concatenate instead of summing

Asked

Viewed 416 times

-3

Well, I need the variable "_som" to return a sum instead of concatenating

_eb.addEventListener('click',function(){
    if (_mon > d.v_eleitor){
        var md = _mon - d.v_eleitor;
        k.innerHTML="$"+md+",00";
        _mon = md;
        _jj.innerHTML = d.x_eleitor;
        if (_pp == ""){
            _pp.setAttribute("value", d.x_eleitor);
        } else {
            var yy = _pp.getAttribute("value");
            var _som = eval("yy + 15");
            alert(_som);
            _pp.setAttribute("value", _som);
        }
    } else {
        alert(d._eleitor);
    }
});

I need it to return the sum in the "value" attribute of this progress bar:

<progress id="progress_bar" value="" max="1000">
  • 2

    Forehead with var _som = parseInt(yy, 10) + 15;. If you have decimal values, test with parseFloat...

  • 1

    Explain better what the code does and assemble an example with jsFiddle. This makes the question clearer and easier to help you.

  • 1

    @He came from the Code Golf... ;)

  • It’s simple, you don’t need to know what variables do, I just need them to disappear instead of concatenate

  • 1

    @Ariel to what you asked, Sergio’s comment already responds.

  • This is also an option var yy = +_pp.getattribute("value"); var _som = yy + 15;

Show 1 more comment

1 answer

2

Sergio has already given you a possible answer to your question. Probably your value is being read as String and therefore cannot be added to a inteiro unconverted.

Change the line:

var _som = eval("yy + 15");

To:

var _som = parseInt(yy, 10) + 15;

Another way is to take out the quotes, and put one + before the yy:

var _som = eval(+yy + 15);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.