I'm trying to loop through nested arrays to determine if an element in the array is either "open" or "senior":
function openOrSenior(data) {
for (let i = 0; i <= data.length; i++) {
let dataElement = data[i];
for (let j = 0; j <= dataElement.length; j++) {
if (dataElement[0] >= 55 && dataElement[1] > 7) {
return ["Senior"];
}
return ["Open"];
}
}
}
Given the input of
[[18, 20],[45, 2],[61, 12],[37, 6],[21, 21],[78, 9]]
The function should output
["Open", "Open", "Senior", "Open", "Open", "Senior"]
But currently it looks like it's only looping through the first element in the main array ([18, 20]
) because my function is only returning:
["Open"]
Why is this function failing to continue to loop through the other nested arrays and return either "Open" or "Senior"? Possibly a problem with the scope?
https://www.codewars.com/kata/categorize-new-member/train/javascript
I was trying to implement what I found here, which suggested a for-loop within a for-loop.
You are returning whenever the check succeeds or not, and you've got a redundant for loop. You should iterate the array with a single for loop, and according to the check push Senior or Open to the result
array. In the end return the result
array.
function openOrSenior(data) {
const result = [];
for (let i = 0; i < data.length; i++) {
const dataElement = data[i];
if (dataElement[0] >= 55 && dataElement[1] > 7) {
result.push("Senior");
} else result.push("Open");
}
return result;
}
console.log(openOrSenior([[18, 20],[45, 2],[61, 12],[37, 6],[21, 21],[78, 9]]));
I have this array air_content: '', compaction_method: 1, concrete_cylinders: [ { id: '', specimen_name: 'A', mould_number: '', curing: 1, age: 7 }, { ...
I have this array air_content: '', compaction_method: 1, concrete_cylinders: [ { id: '', specimen_name: 'A', mould_number: '', curing: 1, age: 7 }, { ...
I'm using angular-nvd3 directive for making a custom line chart display counting number of guest in specific period time range as follow : current Time - 2 --> current Time : will be display as ...
I'm using angular-nvd3 directive for making a custom line chart display counting number of guest in specific period time range as follow : current Time - 2 --> current Time : will be display as ...
how to make setTimeout() work as it looks like in for-loop? like this code function hello() { for (let index = 0; index < 3; index++) { setTimeout(function () { console....
how to make setTimeout() work as it looks like in for-loop? like this code function hello() { for (let index = 0; index < 3; index++) { setTimeout(function () { console....
Promise.prototype.finally has been part of the ECMAScript specification for more than a year and gives no errors in most browsers. When used within Teams desktop app, I see "Promise.prototype....
Promise.prototype.finally has been part of the ECMAScript specification for more than a year and gives no errors in most browsers. When used within Teams desktop app, I see "Promise.prototype....