DEV Community

loading...

Discussion on: Daily Challenge #216 - Rainfall

Collapse
mellen profile image
Matt Ellen

Javascript with regex

function getData(rfd)
{
  let lines = rfd.split('\n');
  let cmap = new Map(lines.map(l => 
                               {
                                 let parts = l.split(':');
                                 return [parts[0], parts[1].split(/[^0-9.]+/)
                                                           .filter(v => v.length > 0)
                                                           .map(v => parseFloat(v))]
                               }));
  return cmap;  
}

function mean(country, rainfalldata)
{
  let d = getData(rainfalldata).get(country);
  if(!d)
  {
    return -1;
  }
  return d.reduce((acc, v) => acc + v, 0) / d.length;
}

function variance(country, rainfalldata)
{
  let m = mean(country, rainfalldata);
  if(m === -1)
  {
    return -1;
  }
  let d = getData(rainfalldata).get(country);
  return d.map(v => (v - m) * (v - m)).reduce((acc, v) => acc + v, 0) / (d.length)
}
Collapse
differentmatt profile image
Matt Lott

Fyi, you can add language specific syntax styling immediately after the first code back-ticks.

github.com/adam-p/markdown-here/wi...

Collapse
mellen profile image
Matt Ellen

Thanks, Matt. I tried that, and if you see my other posts you'll see I was successful, but it's not working for this one for some reason

Collapse
mellen profile image
Matt Ellen

Ah! I hadn't closed the three back ticks!