1

I'm having trouble following the instructions of this problem at codecademy.com. After getting the value stored at 'x' using the data() function, I'm supposed to double it, without using 'var' and then store it at 'y' (using the data() function). I've tried different combinations of 'datafulDiv * 2' to double it, but they've all been wrong. I'm sure this is a simple problem that I'm being very thick about, but if you can help I would be very grateful

$('document').ready(function() {



//get the div. 
//do not add var before this object for testing purposes!!
$datafulDiv = $('#dataful');


//get the value stored at 'x'
$datafulDiv.data('x');


//double it and store it at 'y'. again do not use var!



});
3
  • hint: what would you do if you could declare a var to refer to $datafulDiv.data('x') to calculate two times it's current value? Commented Sep 7, 2012 at 0:36
  • var = var + var , but I'm still not getting it. Commented Sep 7, 2012 at 0:38
  • so then instead of doing var x = ...; x = x + x, everywhere you have x just write $datafulDiv.data('x'). It's the same thing in the end. Commented Sep 7, 2012 at 0:39

1 Answer 1

4
$datafulDiv.data('y', ($datafulDiv.data('x')*2) );
Sign up to request clarification or add additional context in comments.

9 Comments

I may even introduce a parseInt just to be safe, but given the sandbox environment probably not necessary.
parseInt() is not necessary. jQuery.data() already converts to a number if the value is all digits.
Also, FWIW, parseFloat or parseInt would be acceptable (just depends on what X could potentially be).
@BradChristie: If you define a data attribute via .data(), it is stored in a JavaScript object bound to that element. It isn't stored as a string. If you load the data attribute from a real data- attribute, jQuery automatically parses the common data types for you: github.com/jquery/jquery/blob/master/src/data.js#L293. There is no reason to parse it twice.
@BradChristie Yes,but iIt doesn't create a new attribute. jsfiddle.net/tJ3Jn/4
|

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.