1

Supposing I save my image files accordingly, saved in the same folder as the html file, e.g. Card_4_13.jpg, generally Card_"Suit"_"Value".jpg

Is there a way I can use the variables S (1-4) and V (1-13) in order to get the card Card_"S"_"V".jpg?

I intend to avoid creating 4*13 "if" functions to finish this job. For I am yet a beginner, I am curious whether or not this is possible. Any reply is much appreciated

1
  • It's probably worth your time to step back from your current task and work through some basic JavaScript tutorials and/or a good beginner's book and/or course. The task above is very basic. Basic questions are not necessary bad questions or off-topic, but you'll learn more, and more quickly, with a structured tutorial/book/course. Happy coding! Commented Apr 26, 2018 at 12:44

1 Answer 1

1

Yes, you can use nested loops and either string concatenation (adding strings together):

for (var suit = 1; suit <= 4; ++suit) {
  for (var card = 1; card <= 13; ++card) {
    var name = "Card_" + suit + "_" + card + ".jpg";
    console.log(name);
  }
}
.as-console-wrapper {
  max-height: 100% !important;
}

...or a ES2015+ template literal:

for (let suit = 1; suit <= 4; ++suit) {
  for (let card = 1; card <= 13; ++card) {
    const name = `Card_${suit}_${card}.jpg`;
    console.log(name);
  }
}
.as-console-wrapper {
  max-height: 100% !important;
}

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

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.