$(function() {
var getName = $('#fa_welcome').text();
var myName = getName.replace('Welcome ',"");
var a = $('#recent_topics').find('a[href*="/u"]').filter(':contains("'+myName+'")').length;
var b = $('#recent_topics').find('a[href*="/u"]').length;
var c = a-b;
if(c <= 1) {
$('.topics_name').append('<div title="'+c+' New Post" id="newTops">'+c+'</div>');
}
});
Just for randomness I will give you the lengths of var a and b
a= 15
b= 25
I want to subtract these two as in var c
15-25
Though I get -25 parsed?
Any suggestions
Try with parseInt like
var a = parseInt($('#recent_topics').find('a[href*="/u"]').filter(':contains("'+myName+'")').length);
var b = parseInt($('#recent_topics').find('a[href*="/u"]').length);
If you get these values then, use parseInt to convert it in integer,
var c = parseInt(a)-parseInt(b);
use parseFloat for getting result
$(function() {
var getName = $('#fa_welcome').text();
var myName = getName.replace('Welcome ',"");
var a = parseFloat($('#recent_topics').find('a[href*="/u"]').filter(':contains("'+myName+'")').length);
var b = parseFloat($('#recent_topics').find('a[href*="/u"]').length);
var c = a-b;
if(c <= 1) {
$('.topics_name').append('<div title="'+c+' New Post" id="newTops">'+c+'</div>');
}
});
Related
I'm trying to write a function to compute an average of embedded data. I piped the fields in, parse them (they are numbers with decimals, so I can use parseInt). Then I need to check if any of the values are zero, so I filter it. I then want to average the data points that are left over.
Qualtrics.SurveyEngine.addOnload(function()
{
var a = "${e://Fields/a}"
var b = "${e://Fields/b}"
var c = "${e://Fields/c}"
var a= parseFloat(a)
var b= parseFloat(b)
var c= parseFloat(c)
var all_values;
var all_values= [a, b, c];
function isnonzero(value) {
return value != 0;
}
var filter_it;
var filter_it = all_values.filter(isnonzero);
function avg(filter_it){
var sum = 0;
for (var i = 0; i < filter_it.length; i++) {
sum += parseFloat(arr[i])
}
return average= sum / i;
}
Qualtrics.SurveyEngine.setEmbeddedData("total",average);
I don't think my function is right, or the way I tried to get the info out of the function. Any help would be awesome!
You have a couple of problems here, first, you don't need to re-initialize a variable after it has been defined. Second, you never call your avg() function. And third, you can't use the value of i outside of the for loop. YOu also never define arr in the avg() function. And you don't define average outside the avg function Try the following:
Qualtrics.SurveyEngine.addOnload(function()
{
var a = "${e://Fields/a}"
var b = "${e://Fields/b}"
var c = "${e://Fields/c}"
a= parseFloat(a)
b= parseFloat(b)
c= parseFloat(c)
var all_values= [a, b, c];
function isnonzero(value) {
return value != 0;
}
var filter_it;
filter_it = all_values.filter(isnonzero);
function avg(filter_it){
var sum = 0;
for (var i = 0; i < filter_it.length; i++) {
sum += parseFloat(filter_it[i])
}
return sum / filter_it.length;
}
var average = avg(filter_it);
Qualtrics.SurveyEngine.setEmbeddedData("total",average);
});
I have very long code and want to simplify it. I don't think so we can use string concatination to create different variable name. Is there any other way.
If(Something){
var a = some.Test1.x;
var b = some.Test1.y;
var c = some.Test1.z;
var d = some.Test1.p;
}
If(SomethingElse){
var a = some.Test2.x;
var b = some.Test2.y;
var c = some.Test2.z;
var d = some.Test2.p;
}
If(OneMore){
var a = some.Test3.x;
var b = some.Test3.y;
var c = some.Test3.z;
var d = some.Test3.p;
}
May be create some function like this, i know this is not right but anything similar to that.
function test(s){
a = some.s.x;
b = some.s.y;
c = some.s.z;
d = some.s.p;
}
You can use array-like syntax
function test(s) {
var a,b,c,d;
if (s === Something) {
test = "Test1";
}
if (s === SomethingElse) {
test = "Test2";
}
if (s === OneMore) {
test = "Test3";
}
a = some[test].x;
b = some[test].y;
c = some[test].z;
d = some[test].p;
}
This is a much improved version of the functionally equivalent, but never-to-be-used, eval syntax:
eval("a = some."+test+".x");
function test(index){
t = some["Test" + index];
a = t.x;
b = t.y;
c = t.z;
d = t.p;
}
and the execution:
test(1); test(2); test(3)
I try using javascript but the addition is not correct, live code here
function hitung2() {
var a = $(".a2").val();
var b = $(".b2").val();
c = a * b; //a kali b
d = c + a;
$(".c2").val(c);
$(".d2").val(d);
}
function hitung2() {
var a = parseInt($(".a2").val());
var b = parseInt($(".b2").val());
c = a * b; //a kali b
d = c + a;
$(".c2").val(c);
$(".d2").val(d);
}
Or if you going to work with fractions then use parseFloat() function in same way as it described above.
You need to change the string that you get from .val to ints with parseInt()
var a = parseInt($(".a2").val());
var b = parseInt($(".b2").val());
Updated fiddle. http://jsfiddle.net/b39Lz/32/
use parseInt()
var a = $(".a2").val();
var b = $(".b2").val();
temp1=parseInt(a);
temp2=parseInt(b);
c = temp1 * temp2;
d = c + temp1;
Your variables are String, so it uses String + operator, does concatenating the value. Convert string to int using: parseInt("10")
function hitung2() {
var a = $(".a2").val();
var b = $(".b2").val();
c = a * b; //a kali b
d = parseInt(c) + parseInt(a);
$(".c2").val(c);
$(".d2").val(d);
}
Avoid using parseInt() if you dont want to stick to integer calculations only.Try casting to general Number.
function hitung2() {
var a =Number( $(".a2").val());
var b = Number($(".b2").val());
var c = a * b; //a kali b
d =Number(c)+Number(a);
$(".c2").val(c);
$(".d2").val(d);
}
Having this
class="row45"
I would like to make it "row46" using jquery or simple javascript
Tried something like this:
var row = $(this).parents('tr').prev();
var rclass = row.attr('class');
var class_number = rclass.match(/(\d+)/g);
but that only gave me the number 45.
Take that string, convert it to a number, and add one
var class_number = rclass.match(/(\d+)/g)[0];
var num = parseInt(class_number, 10) + 1;
console.log(num); //46
another way with replace
var nextClass = rclass.replace(/(\d+)/g, function(a){ return parseInt(a,10) + 1; });
console.log(nextClass); //row46
You did:
var row = $(this).parents('tr').prev();
var rclass = row.attr('class');
var class_number = rclass.match(/(\d+)/g);
After this simply do:
class_number = parseInt(class_number) + 1;
rclass = "row"+class_number;
row.attr("class", rclass);
And that row will now have class "row46" and rclass will have string "row46".
I've used String.replace(regexp, function) for this kind of thing.
// returns "row46"
"row45".replace(/(\d+)/, function(theString, theNumber) {
return (+theNumber) + 1;
});
How would I add together the values pulled from two separate jQuery.html() calls? Example:
<div id="a">27</div>
<div id="b">3</div>
$(document).ready(function(){
var a = $("#a").html();
var b = $("#b").html();
var c = a + b;
});
All the above does is concatenate var a and b into c (i.e. c = 273) because a & b are strings. How do I get the actual values so that I can properly add them?
You can do either
var a = +$("#a").html();
or
var a = parseInt($("#a").html(), 10);
both of which cast the string to an integer. For more info, see How do I convert a string into an integer in JavaScript?.
Use the parseInt() function:
$(document).ready(function(){
var a = parseInt($("#a").html(), 10);
var b = parseInt($("#b").html(), 10);
var c = a + b;
});
You can use + in both operands
var c = +a + +b;
use text() instead of html().
var x = parseInt( $(el).text(), 10 );