Problem with concat string in jquery animate - javascript

count = 0;
total = 2;
jQuery("#slide").everyTime(5000,function(i){
if(count == total-1) {
count = 0;
jQuery(this).stop().animate({backgroundPosition: "0px 0"}, {duration:1000});
}
else{
jQuery(this).stop().animate({backgroundPosition: "-"+950*count+"px 0"}, {duration:1000});
count++;
}
});
Hi all, i am trying to work on this. there are some problem with the "950*count". When ever i put an operator into this, it wont' work, but if i remove the *count, it work just fine.
Can someone point out what the problem is?
Thank you

Put parentheses around the calculation:
"-" + (950 * count) + "px 0"
Otherwise the expression is evaluated from left to right, first concatenating "-" with "950", then trying to multiply that.

Because there's no such number as -0.

Did you try changing "-"+950*count to "-"+parseInt(950*count) ?

Related

stanford cs101 javascript error in week 5

so i have been slowly working through cs101, first question of week 5 block 4 has me stumped.
so the idea is im supposed to be writing psuedo accurate javascript to determine the number of names starting with A and B in a 2000 entry csv of baby names, im 99% sure i have written the correct code but keep getting returned "unexpected token "{" " i have tried removing all curly brackets one by one which just produces errors obviously and have no idea what i have done wrong, please help.
table = new SimpleTable("baby-2010.csv");
count1 = 0; // A count
count2 = 0; // B count
for (row: table) {
if (row.getField("name").startsWith("A") {
count1 = count1 + 1;
}
if (row.getField("name").endsWith("B") {
count2 = count2 + 1;
}
}
print("A count:", count1);
print("B count:", count2);
all good, VLAZ had the correct solution, both if statements were missing a closing ).
thanks heaps guys
use indexOf and lastIndexOf instead of startsWith and endsWith
if (row.getField("name").indexOf("A") === 0) {
count1 = count1 + 1;
}
if (row.getField("name").lastIndexOf("B") === row.getField('name').length) {
count2 = count2 + 1;
}
You could also use a regex test for /^A/ or /B$/

How to get the NUMBER of same characters from a paragraph using jQuery?

is it possible to get the number of SAME letters from a pararaph using jQuery? so let's say I have this paragraph:
<div>like this printer</div>
I would like to get the number of characters for "i" meaning that I should get 3 since there are 3 "i"s. I was thinking about using $(div:contains(i)), but now sure how to implement this. Any suggestions? Thanks a lot!
//Your string
var str = "I like this printer";
//Shows the count of "i" in your string.
console.log(str.replace(/[^i]/g, "").length);
Happy Coding!
Try this demo:
var src = $('#src').text();
var count = (src.match(/i/g) || []).length;
$('#count').append(count);
alert(count);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="src">like this printer</div>
<p id="count"><strong>Count of "i" = </strong></p>
use .text().split()
$('div').text().split('i').length - 1;
Firstly, you should understand :contains selector. Lastly, you can try this.
$("div:contains('i')").html().split('').reduce(function(p, n) {
return n === 'i' ? ++p : p;
}, 0);
Almost all (all?) of the solutions proposed thus far will fail if the character being searched for is "special" to Regex's (for example, '.').
Here is a method which works regardless of the character being searched for (because it doesn't use a regex):
function countInstancesOf ( str, i ) {
var count = 0;
var pos = -1;
while ((pos = str.indexOf(i, pos+1)) !== -1) ++count;
return count;
}
var howmany = countInstancesOf( $('div').text(), 'i' );
Here is a method based on String.match() and Array.length:
function countSameCharsInElement(char,string){
string.match(new RegExp(char, "g")) || []).length);
}
Sample usage:
countSameCharsInElement('i',$('div').text());
your answering own question. look up contains for strings in JavaScript. an easy to reference for a lot things web is w3 schools.
look there you will find your answer
I recommend you do some more web searching before asking stack overflow.

JavaScript Duplicate Empty Boxes Produced by a loop

Hi I've been having so trouble with this project I need to change colors or matching numbers in 2 arrays, but have the remaining numbers stay there natural color.
for(d = 0; d < lotteryNums.length; d++) {
for(x = 0; x < quickDrawNums.length; x++) {
if(lotteryNums[d] == quickDrawNums[x]) {
quickDrawNums[x] = "<span class='winner'>" + quickDrawNums[x] + "</span>";
winCounter++;
} else {
quickDrawNums[x] = "<span class='number'>" + quickDrawNums[x] + "</span>";
}
}
}
When I have this display it gives me 5 empty boxes and 1 box with the number in it. It also stops my match if from working I was just wondering if anybody could help me sort this out. Thanks for the help in Advance :)
You need to remove the "else" because you are re-writing all the quickDrawNums every time the next lotteryNums is selected. This would result in only on class ='winner' on the last lotteryNums item. Not sure why the empty boxes appear. Verify the original "else" has correct spelling and case for objects, etc.

Javascript/jquery append a variable with a string

I have searched around and don't think I am asking a duplicated question, if I am, I'll delete immediately.
I have this:
$('#dti').text(result3.toFixed(2));
The question is how do I append this with '%'
I am hardcoding a % sign in now but it shows even if there is no value...
$('#dti').text(result3.toFixed(2) + (result3?"%":""));
If result3 can never be emtpy or null but a value >= 0 and you don't want to show 0% then you can try this
$('#dti').text(result3 ? (result3.toFixed(2) + "%") : "");
How about:
$('#dti').text(num = result3.toFixed(2) ? num + '%' : '');

Javascript - transform `elem.style.left` into integer?

I need to make this comparison:
if (x < siteA.style.left || x > siteA.style.left + land.width() ) {
however siteA.style.left returns 20px (e.g) so the condition doesn't work. How do I remove the px and transform it into an integer so I can work with it?
I tried alert(parseInt(siteA.style.left)) and it returned NaN however alert(siteA.style.left) returned 30px, 60px etc.
Use parseInt:
var leftPos = parseInt(siteA.style.left, 10);
where 10 is the base, good practice to always specify it.
Just use parseInt().
Have you tried this?
var left = parseInt(siteA.style.left.replace('px', ''), 10)
When using jQuery you can use $('#siteA').offset().left which returns only the integer value.

Categories