0

I have to return the number of JavaScript developers coming from Europe. I have an array containing 4 objects. (names, country and language etc)

//for reference and to clarify question

var list1 = [
  { firstName: 'Noah', lastName: 'M.', country: 'Switzerland', 
continent: 'Europe', age: 19, language: 'JavaScript' },
  { firstName: 'Maia', lastName: 'S.', country: 'Tahiti', continent: 
'Oceania', age: 28, language: 'JavaScript' },
  { firstName: 'Shufen', lastName: 'L.', country: 'Taiwan', 
continent: 'Asia', age: 35, language: 'HTML' },
  { firstName: 'Sumayah', lastName: 'M.', country: 'Tajikistan', 
continent: 'Asia', age: 30, language: 'CSS' }
];

//my code below

function countDevelopers(list) {

   let euro = 0;

   for( let coder = 0; coder < list.length; coder++ ){
     list[coder]["continent"];
       if( list[coder]["continent"] == 'Europe' ){
         for (let lang = 0; lang < list[coder].length; lang++ ){
           list[lang]["language"];
             if( list[coder][lang]["language"] == 'Javascript'){
               euro++;
             }
         }
       }
   }

   return euro;

}
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
SirNail
  • 85
  • 1
  • 8
  • It would appear the answers to your proposed question are very different to the answers in this question. Implying it is different. However I do not possess, nor seek to possess the knowledge of how to revert the "marked as duplicate". Nor do I feel it truly matters. – SirNail Jul 20 '19 at 12:22

5 Answers5

2

You can achieve this functionally with a reduce call:

let count = list1.reduce(
    (sum, person) => {
        let european_using_javascript = person.continent === 'Europe' && person.language === 'JavaScript';

        // `Number(boolean)` returns `1` if true, and `0` if false
        // Can also use `+european_using_javascript` unary operator, or
        // `european_using_javascript ? 1 : 0`
        return sum + Number(european_using_javascript);
    },

    // Initialize the accumulator to `0`
    0
);

console.log(count);
romellem
  • 5,792
  • 1
  • 32
  • 64
2

Let's walk through what your code is currently doing:

function countDevelopers(list) {

  let euro = 0;
     for( let coder = 0; coder < list.length; coder++ ){

It's looping over the entire list of all coders.

list[coder]["continent"];

This line does absolutely nothing; you can remove it.

if( list[coder]["continent"] == 'Europe' ){
    for (let lang = 0; lang < list[coder].length; lang++ ){

If the current coder is from Europe, you're looping over all the coders again -- wait. Does that seem correct to you? All you need to know is whether THIS coder uses JavaScript, you don't need to bother looking at all the other coders. They don't matter to this coder.

list[lang]["language"];

Another line that does nothing and can be removed.

if( list[coder][lang]["language"] == 'Javascript'){

What is list[coder][lang]? Both coder and lang are variables that are counting through the array index from start to finish. So let's say you're on the second coder (index 1) and the first lang coder (index 0), then this is asking for list[1][0].language . But each element of the list is an object, not an array, so there's no index 0. This line makes no sense.

The last part of the coding, incrementing euro, is fine.

So all you really need is a single loop:

for (let coder = 0; coder < list.length; coder++) {
  if (list[coder].continent === 'Europe' && list[coder].language === 'JavaScript') {
    euro++;
  }
}

(If you want to get fancy, you can make this even more concise with an Array.reduce operation:

let euro = list.reduce((total, coder) => total + (coder.continent === 'Europe' && coder.language === 'JavaScript') ? 1 : 0, 0);

)

IceMetalPunk
  • 5,476
  • 3
  • 19
  • 26
  • thats a really useful walkthrough! Thank you :) – SirNail Jul 19 '19 at 19:21
  • 1
    I used to teach a web development bootcamp, and I had one student who would often struggle because he'd dive right in and focus on the big picture, which made it hard for him to understand the algorithms he was using. I'll give you the same advice I gave him: take it slow. When debugging or creating a new algorithm, break the big picture down into steps. Look at each line of code and make sure you understand what it does before moving onto the next. Computers do everything one step at a time, so you need to think that way to be able to make sure the computer does what you want :) – IceMetalPunk Jul 19 '19 at 19:24
  • 1
    i really appreciate that thanks. Have only been coding for 2 weeks so probably made some goofy mistakes which I guess it to be expected – SirNail Jul 19 '19 at 19:36
1

Iterate the items, and if the user's continent is Europe, and the language is Javascript increment the count by 1:

function countDevelopers(list) {
  let count = 0;

  for(var i = 0; i < list1.length; i++){
    const user = list1[i];
    if(user.continent === 'Europe' && user.language === 'JavaScript') {
      count += 1;
    }
  }

  return count; 
}

const list1 = [{"firstName":"Noah","lastName":"M.","country":"Switzerland","continent":"Europe","age":19,"language":"JavaScript"},{"firstName":"Maia","lastName":"S.","country":"Tahiti","continent":"Oceania","age":28,"language":"JavaScript"},{"firstName":"Shufen","lastName":"L.","country":"Taiwan","continent":"Asia","age":35,"language":"HTML"},{"firstName":"Sumayah","lastName":"M.","country":"Tajikistan","continent":"Asia","age":30,"language":"CSS"}];

const result = countDevelopers(list1);

console.log(result);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
1

Here are two ways to do it, one with foreach another with reduce.

   var list1 = [{
    firstName: 'Noah',
    lastName: 'M.',
    country: 'Switzerland',
    continent: 'Europe',
    age: 19,
    language: 'JavaScript'
},
{
    firstName: 'Maia',
    lastName: 'S.',
    country: 'Tahiti',
    continent: 'Oceania',
    age: 28,
    language: 'JavaScript'
},
{
    firstName: 'Shufen',
    lastName: 'L.',
    country: 'Taiwan',
    continent: 'Asia',
    age: 35,
    language: 'HTML'
},
{
    firstName: 'Sumayah',
    lastName: 'M.',
    country: 'Tajikistan',
    continent: 'Asia',
    age: 30,
    language: 'CSS'
}
];

const countDev = (x) => {
  let count = 0;
  x.forEach(x => {
      if (x.continent === "Europe" && x.language === "JavaScript") count++
  })
  return count;
}

console.log(countDev(list1))

// OR

const countDev1 = (x) => {

  return x.reduce((accum, x) => {
    if (x.continent === "Europe" && x.language === "JavaScript") return ++accum;
    return accum;
  }, 0)

}

console.log(countDev1(list1))
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
0

You could use filters and have a generic function that supplies the country and language

    var list1 = [
      { firstName: 'Noah', lastName: 'M.', country: 'Switzerland', 
    continent: 'Europe', age: 19, language: 'JavaScript' },
      { firstName: 'Maia', lastName: 'S.', country: 'Tahiti', continent: 
    'Oceania', age: 28, language: 'JavaScript' },
      { firstName: 'Shufen', lastName: 'L.', country: 'Taiwan', 
    continent: 'Asia', age: 35, language: 'HTML' },
      { firstName: 'Sumayah', lastName: 'M.', country: 'Tajikistan', 
    continent: 'Asia', age: 30, language: 'CSS' }
    ];

    function countDevelopers(list, continent, language) {
return list.filter(developer => {return developer.continent == continent && developer.language == language}).length
    }


console.log(countDevelopers(list1, 'Europe', 'JavaScript'));
Satej S
  • 2,113
  • 1
  • 16
  • 22