Ad
Code
Diff
  • function dumbRockPaperScissors(player1, player2) {
      function checkForWinner(incoming) {
        const winnerLookup = {
          'Draw': [
            'RockRock',
            'PaperPaper',
            'ScissorsScissors',
          ],
          'Player 1 wins': [
            'RockScissors',
            'ScissorsPaper',
            'PaperRock',
          ],
          'Player 2 wins': [
            'ScissorsRock',
            'PaperScissors',
            'RockPaper',
          ],
        }
        const results = Object.keys(winnerLookup)
        
        for(let i = 0; i < results.length; i++) {
          const winningConditions = winnerLookup[results[i]];
          for(let j = 0; j < winningConditions.length; j++){
            const condition = winningConditions[j];
            for(let k = 0; k < condition.length; k++){
              if (condition[k] === fight[k]) {
                if (k === condition.length - 1) {
                  return results[i];
                }
                continue;
              }
              break;
            }        
          }
        }
      }
      const fight = [];
      for (let i = 0; i < player1.length; i++) {
        fight.push(player1[i]);
        const winner = checkForWinner(fight);
        if (winner) {
          return winner;
        }
      }
      
      for (let i = 0; i < player2.length; i++) {
        fight.push(player2[i]);
        const winner = checkForWinner(fight);
        if (winner) {
          return winner;
        }
      }
      
    }
    • function dumbRockPaperScissors(player1, player2) {
    • function checkForWinner(incoming) {
    • const winnerLookup = {
    • 'Draw': [
    • 'RockRock',
    • 'PaperPaper',
    • 'ScissorsScissors',
    • ],
    • 'Player 1 wins': [
    • 'RockScissors',
    • 'ScissorsPaper',
    • 'PaperRock',
    • ],
    • 'Player 2 wins': [
    • 'ScissorsRock',
    • 'PaperScissors',
    • 'RockPaper',
    • ],
    • }
    • const results = Object.keys(winnerLookup)
    • for(let i = 0; i < results.length; i++) {
    • const winningConditions = winnerLookup[results[i]];
    • for(let j = 0; j < winningConditions.length; j++){
    • const condition = winningConditions[j];
    • for(let k = 0; k < condition.length; k++){
    • if (condition[k] === fight[k]) {
    • if (k === condition.length - 1) {
    • return results[i];
    • }
    • continue;
    • }
    • break;
    • }
    • }
    • }
    • }
    • const fight = [];
    • for (let i = 0; i < player1.length; i++) {
    • fight.push(player1[i]);
    • const winner = checkForWinner(fight);
    • if (winner) {
    • return winner;
    • }
    • }
    • for (let i = 0; i < player2.length; i++) {
    • fight.push(player2[i]);
    • const winner = checkForWinner(fight);
    • if (winner) {
    • return winner;
    • }
    • }
    • }
Code
Diff
  • const firstNonRepeatingCharacter = (str) => {
        const lookup = {};
        let potentialWinners = [];
        for(let i = 0; i < str.length; i++) {
            const c = str[i];
          if(lookup[c] && lookup[c] == 1){
            potentialWinners = potentialWinners.filter(pw => pw !== c);
            lookup[c] = 2;
            continue;
          }
          lookup[c] = 1;
          potentialWinners.push(c);
        }
        return potentialWinners.length == 0 ? null : potentialWinners[0]; // return null if no unique character is found
    };
    • const firstNonRepeatingCharacter = (str) => {
    • for (let i = 0; i < str.length; i++) {
    • let seenDuplicate = false;
    • for (let j = 0; j < str.length; j++) {
    • if (str[i] === str[j] && i !== j) {
    • seenDuplicate = true;
    • break;
    • }
    • }
    • if (!seenDuplicate) {
    • return str[i];
    • }
    • const lookup = {};
    • let potentialWinners = [];
    • for(let i = 0; i < str.length; i++) {
    • const c = str[i];
    • if(lookup[c] && lookup[c] == 1){
    • potentialWinners = potentialWinners.filter(pw => pw !== c);
    • lookup[c] = 2;
    • continue;
    • }
    • lookup[c] = 1;
    • potentialWinners.push(c);
    • }
    • return null; // return null if no unique character is found
    • return potentialWinners.length == 0 ? null : potentialWinners[0]; // return null if no unique character is found
    • };