3

What am I going wrong here? I'm trying to convert a string to an integer to use in addition.

I have an image with a class of portfolio and a src of images/portfolio-images/gbn1.jpg

The first alert says the value of imageInt is 1, but the second alert says the value of newImg is gbn11.jpg when I'm trying to get gbn2.jpg.

I am posting my code below:

var mainImg = $('.active-more').closest(".container").find("img.portfolio:first").attr("src");
var nofilename = mainImg.replace("images/portfolio-images/",""); 
nofilename = nofilename.replace(".jpg",""); 
var imageString = nofilename.replace(/[A-Za-z$-]/g, "");
var imageInt = parseInt(imageString, 10);   
var imageProject = nofilename.replace(/\d+/g, '');  
alert(imageInt);
var newImg= imageProject + imageInt + 1 + ".jpg";
alert(newImg);
1
  • +1 for naming variables in sample to show type. Commented Dec 20, 2012 at 5:49

3 Answers 3

6

You are doing string concatenation due to order of + - add braces to fix:

 var newImg= imageProject + (imageInt + 1) + ".jpg";
Sign up to request clarification or add additional context in comments.

4 Comments

@maskedj: try my answer below
@lwburk, not sure where do you see a problem: string + int or int + string produces string, but int + int produces int (Number to be precise). So when one forces (imageInt + 1) to be executed first it will be int+int, and than go to string+int/ int+string route.
@lwburk :) no problem, it is actually realatively surprising to see variable name to actually reflects what is stored there.
@AlexeiLevenkov - I work on legacy code that extensively uses Hungarian notation, so I actually see it a lot. No excuses here :)
3

Try this :

var newImg= imageProject + (imageInt + 1) + ".jpg";

The + operator is used to add numbers and to concatenate strings. If the left operand is a string then the right one is converted to a string.

In the above, by enclosing the imageInt + 1 in parentheses we ensure that it is evaluated as an integer before being concatenated as a string.

See the fiddle : http://jsfiddle.net/VQG5j

1 Comment

@lwburk—you can delete comments if you wish.
0

You try the adding stuff separately and concatenate it to the image name, use parseInt on adding too.

Try the code construct given below:

var mainImg = $('.active-more').closest(".container").find("img.portfolio:first").attr("src");
var nofilename = mainImg.replace("images/portfolio-images/",""); 
nofilename = nofilename.replace(".jpg",""); 
var imageString = nofilename.replace(/[A-Za-z$-]/g, "");
var imageInt = parseInt(imageString, 10);   
var imageProject = nofilename.replace(/\d+/g, '');  
alert(imageInt);
imgIntNew = imageInt + parseInt(1);
alert(imageIntNew);
var newImg= imageProject + imgIntNew + ".jpg";
alert(newImg);

Comments

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.