简体   繁体   中英

Why will my jQuery not work when I add the .sqrt()

I am trying to create a grid of divs and need to take the square root of the users input to find the height and width needed to make a square. This is the code I have doing it:

$('#size').click(function(){
 $('.container').empty();
 var big = prompt('Enter the desired number of cubes');
 var final = $(big).sqrt();
 drawGrid(final);

Here is a link to my whole project on jsfiddle: https://jsfiddle.net/ztwsptys/ Any help would be appreciated.

Just do Math.sqrt(big) . Assuming that you are sure that big is a Number . You don't need to go crazy with wrapping your big variable with jQuery. Make sure that big is actually a number. If you read the user input I can bet that the value is read as a string. You would need to ParseFloat or ParseInt it depending what kind of value you are expecting from the user.

Use Math.sqrt(big); instead of $(big).sqrt();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM