<commit_before>'use strict';

let montyHall = function () {
  let selectRandomDoor = () => Math.floor(Math.random() * 3); // => 0, 1 or 2

  let generateDoors = function () {
    let doors = ['goat', 'goat', 'goat'];
    let doorWithPrize = selectRandomDoor();
    doors[doorWithPrize] = 'car';
    return doors;
  };

  let findNoPrizeDoorToOpen = function (doors, firstDoor) {
    let doorToOpen = selectRandomDoor();

    if (doorToOpen === firstDoor || doors[doorToOpen] === 'car') {
      return findNoPrizeDoorToOpen(doors, firstDoor) // Is not an unpicked 'goat' door. Trying again.
    }
    return doorToOpen;
  };

  let findLastDoor = function (firstDoor, secondDoor) {
    let sumOfDoors = 3;                                     // The doors numbers are 0, 1 and 2, i.e. 0+1+2=3
    let unopenedDoor = sumOfDoors - firstDoor - secondDoor; // E.g if the unopened door is number 1, 3-0-2=1
    return unopenedDoor;
  };

  let doors = generateDoors();                                            // Will generate 3 doors
  let chooseFirstDoor = selectRandomDoor();                               // Contestant picks a door  
  let openSecondDoor = findNoPrizeDoorToOpen(doors, chooseFirstDoor);     // Host opens a door with a goat
  let switchToThirdDoor = findLastDoor(chooseFirstDoor, openSecondDoor);  // Contestant switches door
  let whatIsBehindThirdDoor = doors[switchToThirdDoor];                   // Contestant sees what is behind the door
  return whatIsBehindThirdDoor;                                           // Will return 'car' or 'goat'
};

let runMontyHallSimulation = function (iterations) {
  let results = {
    "car": 0,
    "goat": 0
  };

  for (let i = 0; i < iterations; i++) {
    results[montyHall()]++
  };

  console.log('Iterations      :', iterations)
  console.log('Percent of car  :', ((results.car / iterations) * 100).toFixed(2), '%')
  console.log('Percent of goat :', ((results.goat / iterations) * 100).toFixed(2), '%')
}

let iterations = process.argv[2] || 1000;
runMontyHallSimulation(iterations);<commit_msg>Made "const" the default identifier instead of "let". Made functions more "pure"<commit_after>'use strict';

const montyHall = function () {
  const selectRandomDoor = () => Math.floor(Math.random() * 3); // => 0, 1 or 2

  const generateDoors = function (doorWithPrize) {
    let doors = ['goat', 'goat', 'goat'];
    doors[doorWithPrize] = 'car';
    return doors;
  };

  const findNoPrizeDoorToOpen = function (doors, firstDoor) {

    const findRoomNumberOfValidDoors = function(value, key) {
      const alreadySelectedDoor = this;
      return (key === alreadySelectedDoor || value === "car") ? value : key;
    }
    const removeInvalidDoors = (value) => Number.isInteger(value);
    const selectFirstValidDoor = (val) => val;

    // => MAP:    If the door can be opened; replaces value, i.e. "car"/"goat", for the door number, i.e. 0-2
    // => FILTER: Removes all values, leaving only room numbers
    // => REDUCE: Returns the first valid door number availible
    const doorToOpen = doors.map(findRoomNumberOfValidDoors, firstDoor).filter(removeInvalidDoors).reduce(selectFirstValidDoor);
    return doorToOpen;
  };

  const findLastDoor = function (firstDoor, secondDoor) {
    const sumOfDoors = 3;                                     // The doors numbers are 0, 1 and 2, i.e. 0+1+2=3
    const unopenedDoor = sumOfDoors - firstDoor - secondDoor; // E.g if the unopened door is number 1, 3-0-2=1
    return unopenedDoor;
  };

  const doorWithPrize = selectRandomDoor();                                 // Randomly select a door containing the prize
  const doors = generateDoors(doorWithPrize);                               // Generate 3 doors. Behind one door is a car; behind the others, goats
  const chooseFirstDoor = selectRandomDoor();                               // Contestant randomly picks a door
  const openSecondDoor = findNoPrizeDoorToOpen(doors, chooseFirstDoor);     // Host opens a door with a goat
  const switchToThirdDoor = findLastDoor(chooseFirstDoor, openSecondDoor);  // Contestant switches door
  const whatIsBehindThirdDoor = doors[switchToThirdDoor];                   // Contestant sees what is behind the door
  return whatIsBehindThirdDoor;                                             // Will return 'car' or 'goat'
};

const runMontyHallSimulation = function (iterations) {
  let results = {
    "car": 0,
    "goat": 0
  };

  for (let i = 0; i < iterations; i++) {
    results[montyHall()]++
  };

  console.log('Iterations      :', iterations)
  console.log('Percent of car  :', ((results.car / iterations) * 100).toFixed(2), '%')
  console.log('Percent of goat :', ((results.goat / iterations) * 100).toFixed(2), '%')
}

const iterations = process.argv[2] || 1000;
runMontyHallSimulation(iterations);
{'fraction_non_alphanumeric': 0.11980897009966777, 'fraction_numerical': 0.014534883720930232, 'mean_word_length': 2.711093990755008, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2675113', 'n_tokens_mistral': 1583, 'n_tokens_neox': 1459, 'n_words': 416}