All times are UTC - 6 hours   |  Page 1 of 1

Dion Designs

Calculating the ratio... (to Javascript experts)

Calculating the ratio... (to Javascript experts)

Posted: February 2nd 2014, 7:48pm
by Hossam
There are 2 Divs: #Thanks_given li and #Thanks_received li.

But i want make this one; Thanks ratio (between the Thanks received and the Thanks given), with img bar and Css.

for example:

User:

Thanks given: 6000
Thanks received: 720
Thanks ratio: 0.2 (1200/6000=0.12) with img bar.


if the Thanks ratio= 0.12 the img bar= (full red bar) (Minimum ratio). with CSS.
if the Thanks ratio= 0.2 the img bar= IMG.
if the Thanks ratio= 0.3 the img bar= IMG.
if the Thanks ratio= 0.4 the img bar= IMG.
if the Thanks ratio= 0.5 the img bar= IMG.
if the Thanks ratio= 0.6 the img bar= IMG.
if the Thanks ratio= 0.7 the img bar= IMG.
if the Thanks ratio= 0.8 the img bar= IMG.
if the Thanks ratio= 0.9 the img bar= IMG.
if the Thanks ratio= 1.0 (and more) the img bar= (full green bar).

Calculating the ratio... (to Javascript experts)

Posted: February 3rd 2014, 1:28pm
by Dion
Here's something to get you started:

Code: Select all
var ratio = received / given;
var img_src = '(URL of full red image)';
if (ratio >= 0.2) {
img_src = '(URL of 0.2 image)';
}
elseif (ratio >= 0.3) {
img_src = '(URL of 0.3 image)';
}
elseif (ratio >= 0.4) {
img_src = '(URL of 0.4 image)';
}
elseif (ratio >= 0.5) {
img_src = '(URL of 0.5 image)';
}
elseif (ratio >= 0.6) {
img_src = '(URL of 0.6 image)';
}
elseif (ratio >= 0.7) {
img_src = '(URL of 0.7 image)';
}
elseif (ratio >= 0.8) {
img_src = '(URL of 0.8 image)';
}
elseif (ratio >= 0.9) {
img_src = '(URL of 0.9 image)';
}
else {
img_src = '(URL of full green image)';
}
(now use the img_src variable to display the image)

I'm sure there are better ways to accomplish this, but this is the easiest, and the easiest to understand.

Calculating the ratio... (to Javascript experts)

Posted: February 3rd 2014, 4:23pm
by Hossam
Good. but this to show the ratio, right? but how to Calculate the ratio and giving it id?

Calculating the ratio... (to Javascript experts)

Posted: February 26th 2014, 10:49am
by Hossam
Well, I made something, but still does not work:

Code: Select all
$(document).ready(function(){
var given = $("#Thanks_given");
var received = $("#Thanks_received");
var ratio = given / received;
if (ratio < 1) {
$(function() {
$('#Thanks_received').after('<div style="background:#b71604;color: #ffffff;font-size: 10px;font-weight: 800;padding: 0 3px;display:inline;">High Ratio</div>');
});
});
}
});


any ideas why this does not work?