0

I am pulling out data from bitcoinaverage.com's API and saving it in var 'price'.

Later I want to use it in a calculation im doing in JS but it doesn't work, only works if I put a static integer instead of dynamic 'price'.

I've tried pasteInt() on price and it still doesn't work. Any ideas on what I could be doing wrong? https://jsfiddle.net/7b2jaLxh/

var xbtc = new XMLHttpRequest();
xbtc.open('GET', 'https://api.bitcoinaverage.com/ticker/global/CAD/', true);
xbtc.onreadystatechange = function(){
    if(xbtc.readyState == 4){
        var ticker = JSON.parse(xbtc.responseText);
        price = ticker.last;
    }
};
xbtc.send();

here's the JS where im using price variable: (at the bottom)

var directionSlider = document.getElementById('slider-direction');

noUiSlider.create(directionSlider, {
    start: 20,
    connect: [true, false],
    direction: 'ltr',
    range: {
        'min': 2,
        'max': 99.99
    }
});
var cadc = document.getElementById('cadc');
var btcc = document.getElementById('btcc');

directionSlider.noUiSlider.on('update', function( values, handle ) {
    cadc.value = directionSlider.noUiSlider.get();
    cadConvert();
});

cadc.addEventListener('change', function(){
    directionSlider.noUiSlider.set(this.value);
    cadConvert();
});
btcc.addEventListener('change', function(e){
   directionSlider.noUiSlider.set(this.value*price);
});
function cadConvert() {
    var cad = directionSlider.noUiSlider.get();
    var cadCalc = cad / price;
    document.getElementById("btcc").value = cadCalc;

}
4
  • where is price being initially declared? It seems like you are probably trying to use price outside of its scope Commented Feb 13, 2017 at 0:52
  • I've saved it in a file named price.js but I called <script src="js/price.js"></script> in the html where I run the js. Commented Feb 13, 2017 at 0:54
  • Can you tell us what the value of price is in your cadConvert() function? Set a breakpoint or do a console.log. Commented Feb 13, 2017 at 1:00
  • 1308.74 CAD, I output it like this: document.getElementById('btc').innerHTML = price + " CAD"; and it works Commented Feb 13, 2017 at 1:03

1 Answer 1

1

Try changing the cad variable assignment to this:

var cad = parseFloat(directionSlider.noUiSlider.get());

That's because the slider is most likely returning the value as a string, rather than as a floating-point number.


UPDATED ANSWER:

I did a console.log(cad, typeof cad); and it was indeed a string value that's being returned from your slider, so I changed the code to the following:

function cadConvert() {
    if(!price) return;
    var cad = parseFloat(directionSlider.noUiSlider.get());
    var cadCalc = cad / price;
    document.getElementById("btcc").value = cadCalc;
}

I tested, and your slider is working fine now. Try this updated fiddle:

https://jsfiddle.net/5pxvvmdL/

Also, I added a var price; declaration at the top near your other declarations, just for code readability purposes, as even though when you assign a variable in Javascript without using var it automatically gets declared as a global variable, it's best if you explicitly do that in your code by declaring your global variable literrally using var in the global scope (outside of any functions). As mentioned, just for code readability purposes.

Additonally, I commented out the document.getElementById('btc').innerHTML = "Global Market: $" + (price).toFixed(2) + " CAD"; line in price.js as there's no "btc" layer in the document and thus it was throwing an error.

Finally, I added a call to cadConvert() on the onreadystatechange event so that the default Bitcoin price is displayed as soon as it's loaded, using the default slider price.

Sign up to request clarification or add additional context in comments.

2 Comments

Try adding this code right after cadConvert(){ so you can see exactly what values and of which types are being received: console.info('cad: ', cad, (typeof cad)); console.info('price: ', price, (typeof price)); Then paste here in a comment the output so we can see what's going on.
You saved the day ablopez! Thank you so much.

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.