In a function, I have an object that contains a mix of letters and numbers. The function is designed to take in an array of numbers, and then run a for-in loop that checks if any of the values in the array match the numbers stored in the object. If a match is found, the function should return only the corresponding letter key.
For example, calling switcher(['26'])
should return 'a'. Is achieving this outcome possible?
function switcher(x){
const letters = {
a: '26',
b: '25',
c: '24',
d: '23',
e: '22',
f: '21',
g: '20',
h: '19',
i: '18',
j: '17',
k: '16',
l: '15',
m: '14',
n: '13',
o: '12',
p: '11',
q: '10',
r: '9',
s: '8',
t: '7',
u: '6',
v: '5',
w: '4',
x: '3',
y: '2',
z: '1'
};
}
I've tried using the ES6 map()
method to accomplish this task, but I'm unsure about what logic to include within my if statement. Here's what I have so far:
return x.map(function(number){
let keys = Object.keys(letters);
for(var key in letters){
if(letters[key] === number){
}
}
});
}
Do you know of a simpler way to achieve this functionality?