DEV Community

Discussion on: Daily Challenge #173 - Pandemia

Collapse
 
madza profile image
Madza • Edited

js

// Please specify Input conditions:

// Will 'X' always be Uppercase in string?
// Will str always consist just of '1', 'X' and '0'?

// Assuming BOTH above are TRUE:

function getPercentage(str){

  if(!['0','1'].some(el=>str.includes(el))){return '0';}
  if (!str.includes('X')){return '0%'};

  var continents = str.split('X');

  var totalPeople = 0;
  var totalInfected = 0;

  for(var i = 0; i<continents.length; i++){
    if(continents[i].includes('1')){
      totalInfected+=continents[i].length;
    }
    totalPeople+=continents[i].length;
  }

  return 100*totalInfected/totalPeople;

}

// Maps without people are strings without '1' AND '0'. Fix that.
// 'No people case' should return false and 'just land case' - number type 0.
// Hide the percentage formula as that should be part of task.