0

How to create function replace number to image? Example: var dd="123" I want to create function replace value of dd to image.

if 1=image1
if 2=image2
if 3=image3

How to do that? Please provide coding.

0

5 Answers 5

1

function imageReplace ( str ) {
    var arr = str.split('');
    var returnArr = new Array();
    for(var i in arr ){
        returnArr.push("image"+arr[i]);
    }
    return returnArr;
}

I think thats what your looking for

Sign up to request clarification or add additional context in comments.

5 Comments

Dear your coding not working. dd=123 how to create function convert 123 to image if 1=image1 if 2=image2 if 3=image3 best regards
I don't know what that means... perhaps updating the question with a little more info. I just took a stab at what I thought you meant.
dear sir. example var dd=312 .so i want to convert dd value to image.if dd=3 so ur coding catch image3.gif, if dd=1 ur coding catch image1, if dd=2 so ur coding catch image2. Best regards
Result=dd=312 (image3.gif,image1.gif,imag2.gif)
this function returns an array like this, ['image3', 'image2', 'image1'] you want it to return a string like "312 (image3.gif,image1.gif,imag2.gif)"?
0

Hard to answer because of the lack of expected flow control, but something like this:

function concatFilename(str,index)
{
   return 'image'+str[index];
}

Usage:

var dd = 'abc123';
for (var i=0; i < dd.length; i++)
{
  console.log(concatFilename(dd, index));      
}

Or... easier:

dd[2] = 'c';

Comments

0

Does dd contain a single number, to be converted as a whole? If so:

var dd = '123';
dd.replace(/([\d]+)/, /image$1/);  // 'image123'

If you want each digit to be converted:

var dd = '123';
dd.replace(/([\d])/, /image$1/);  // 'image1image2image3'

Hope that helps. :)

Honestly, I'm assuming you almost certainly want the first of the two. (If in doubt, ask yourself -- what behavior do you expect if you're on image 10 or 11?)

4 Comments

some dd=34433 is random number sir.
Then you want the first of the two code samples I offered. It will work for any arbitrary positive integer.
can you provide full script ?
What full script? I only wrote code specifically to answer your question.
0

You can use a regular expression to replace each digit with an image tag:

dd = dd.replace(/(\d)/g, '<img src="image$1.gif" alt="$1" />');

This also works on a string containing numbers, like "Page 12 of 42" is turned into "Page <img src="image1.gif" alt="1" /><img src="image2.gif" alt="2" /> of <img src="image4.gif" alt="4" /><img src="image2.gif" alt="2" />".

Comments

0
var getImages = function(s) {
  var images=[], ss=(""+s).split(''), len=ss.length, i;
  for (i=0; i<len; i++) {
    images.push("image" + ss[i] + ".gif");
  }
  return images;
};
getImages(123); // => ["image1.gif", "image2.gif", "image3.gif"]
getImages(42); // => ["image4.gif", "image2.gif"]
getImages(0); // => ["image0.gif"]

[Edit] or perhaps something like this?

var getImageNames = function(s) {
  return (""+s).split('').map(function(x) {
    return '"image'+x+'.gif"';
  }).join(' ');
}
getImageNames(123); // => '"image1.gif" "image2.gif" "image3.gif"'

4 Comments

Dear if i want to replace , to space. how can sir?
@user: something like getImages(123).join(' ')? I am not sure what you are asking - please update your question with more details for all to see.
this is result ["image4.gif", "image2.gif"] i dont need symbol (,).how can i replace , to blank....best regards
@user: the result is an array, try using its "join" function, per my updated answer.

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.