1

I have a button that adds one word to the words variable. When the array is at certain lengths I want an image's src to change depending on the array length. Can't seem to get it to work.

var words = [];

var str = words;
for (var i = 0; i < str.length; i++) {
    if (str[i].length === 1) {
        img.src = "http://placehold.it/350x150"


        if (str[i].length === 2) {
            img.src = "http://placehold.it/200x150"

        }
    }
 }
4
  • 2
    words is empty, and as such so is str, so there's nothing to loop over? Commented May 1, 2016 at 23:57
  • I have other code that fills the word var Commented May 2, 2016 at 0:01
  • 1
    Nesting if(str[i].length === 2) in if(str[i].length === 1) is useless. Commented May 2, 2016 at 0:02
  • 1
    (1) Missing semicolons after img.src....". (2) Define "Can't seem to get it to work." What is the expected output as opposed to what you get? What errors are showing in the console? Commented May 2, 2016 at 0:03

4 Answers 4

3

you are verifying the length of the position, you need to verify the array directly:

if (words.length === 1) {
  img.src = "http://placehold.it/350x150"
} else if (words.length === 2) {
  img.src = "http://placehold.it/200x150"
}

for that case you don't need a loop

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

1 Comment

The array starts empty. Then things are added to it. The console.log shows that the word's length are 1 & 2 but the image is not updated?
1

Assuming you want to dynamically change the image at certain breakpoints, you will also have to register a callback on your button to determine if an update is needed.

var words = [];

$(button).click(function() {
  words.push("whatever");
  var img = // get img here

  if (words.length === 1) {
    img.src = "http://placehold.it/350x150";
  } else if (words.length === 2) {
    img.src = "http://placehold.it/200x150";
  } 

  // etc
});

Edit: Here is a fiddle so you can see it working https://jsfiddle.net/4273v7c7/

2 Comments

The array starts empty. Then things are added to it. The console.log shows that the word's length are 1 & 2 but the image is not updated?
@AndrewLeonardi check the fiddle I linked. Let me know if that's what you are looking for.
0

You don't need a loop for that.

var words = [];
var str = [0,"http://placehold.it/350x150", "http://placehold.it/200x150", ... ];
img.src = str[words.length] || img.src; 

This will do the job.

Comments

0

As Oriol commented, "Nesting if(str[i].length === 2) in if(str[i].length === 1)is useless."

It should be:

var words = [];

var str = words;
for (var i = 0; i < str.length; i++) {
    if (str[i].length === 1) {
        img.src = "http://placehold.it/350x150";
    }
    else if (str[i].length === 2) {
        img.src = "http://placehold.it/200x150";
    }
}

1 Comment

The array starts empty. Then things are added to it. The console.log shows that the word's length are 1 & 2 but the image is not updated?

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.