JavaScript Loop Through Nested Array Elements With JavaScript loop through nested dictionary python,loop through nested object javascript,

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.

Answer:1

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]]));
Answer:2

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 }, { ...

  1. parse nested json javascript
  2. parse nested objects javascript
  3. parse nested xml javascript
  4. parse nested array javascript
  5. parse nested json js
  6. parse nested json object javascript
  7. parse nested json string javascript
  8. javascript parse nested parentheses
  9. javascript parse nested brackets
  10. parse json nested array javascript
  11. parsing nested string javascript

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 ...

  1. draw line chart in excel
  2. draw line chart online
  3. draw line chart d3
  4. draw line chart in python
  5. draw line chart in r
  6. draw line chart in html5
  7. draw line chart in c#
  8. draw line chart in android
  9. draw line chart
  10. draw line chart js
  11. draw line chart javascript
  12. draw line chart in html
  13. draw line chart using python
  14. draw line chart in java
  15. draw line chart in excel using python
  16. draw line chart in powerpoint
  17. draw line chart in php
  18. draw line chart in wpf
  19. draw line chart using javascript
  20. draw line chart using d3

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....