function openOrSenior(data) {
let newArr = []
let userData = [data]
return userData.forEach(data => {
data.map(data => {
let answer = (data[0] >= 55 && data[1] > 7) ? console.log("Senior") : console.log("Open");
return answer
})
})
}
the above function should either display senior or open in this form [ 'Open', 'Senior', 'Open', 'Senior' ]
the outpout i got instead was:
Open
Senior
Open
Senior
an example of what is expected:
input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]
output = ["Open", "Open", "Senior", "Open", "Open", "Senior"]
CodePudding user response:
You could just map the wanted strings, depending on the values of left an right items of the nested arrays.
function openOrSenior(data) {
return data.map(([l, r]) => l >= 55 && r > 7 ? 'Senior' : 'Open');
}
const data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]];
console.log(openOrSenior(data)); // ["Open", "Open", "Senior", "Open", "Open", "Senior"]
CodePudding user response:
If you would like to use the same code-template that you have provided in the description then you can solve it in this way:
function openOrSenior(data) {
let newArr = []
let userData = [data]
userData.forEach(data => {
data.map(dataInside => {
let answer = (dataInside[0] >= 55 && dataInside[1] > 7) ? "Senior" : "Open"
newArr.push(answer)
})
})
return newArr
}
const data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]];
console.log(openOrSenior(data)); // ["Open", "Open", "Senior", "Open", "Open", "Senior"]
But in above solution, we are performing redundant operations, the better way to solve it will be this one:
function openOrSenior(data) {
return data.map(dataInside => (dataInside[0] >= 55 && dataInside[1] > 7) ? "Senior" : "Open")
}
const data = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]];
console.log(openOrSenior(data)); // ["Open", "Open", "Senior", "Open", "Open", "Senior"]