I have dynamic input array variable value
Decode values
1–>Java
2–>HTML
3–>SQL
4–>PHP
5–>DOTNet
6–>JavaScript
7–>Oracle
input values
arrstrval =1,2,5,4,7
but my output should be like below
arrstrval =Java,HTML,DOTNet,PHP,Oracle
Can any one help me Please
Can you show what you’ve tried, because this doesn’t make a lot of sense atm (what’s a dynamic input array variable? And arrays are zero-indexed, they don’t start from 1). Can you show some actual code? Something like this will do it, but I don’t know context or usage so shooting in the dark:
function decode (inputStr, langs = ["Java", "HTML", "SQL", "PHP", "DOTNet", "JavaScript", "Oracle"]) {
const selectedLangs = [];
const parsedInput =
inputStr
.trim()
.split(",")
.filter(val => /^\d+$/.test(val))
.map(val => parseInt(val, 10));
for (const val of parsedInput) {
if (val <= langs.length) {
selectedLangs.push(langs[val]);
}
}
return selectedLangs;
}