Ad
Code
Diff
  • class Rule {
      w = '';
      l = '';
      
      prepare=function(w, l) {
        this.setWinner(w);
        this.setLoser(l);
      }
    
      setWinner=function(w) {
        this.w = w;
      }
    
      setLoser=function(l) {
        this.l = l;
      }
    
      winner=function() {
        return this.w;
      }
    
      loser=function() {
        return this.l;
      }
    }
    
    class Determination {
      rules = [];
      
      prepare=function(rules) {
        this.rules = rules;
      }
      
      winner=function(item1, item2) {
        let my_rule = new Rule();
        my_rule.prepare(item1, item2);
        
        let winner = null;
        for(let i=0; i<this.rules.length; i++) {
          rule = this.rules[i];;
          if(rule.winner() == my_rule.winner()) {
            if(rule.loser() == my_rule.loser()) {
              winner = rule.winner();
            }
          }
        }
        
        if(winner != null && winner != "") {
          return "Player 1 wins";
        }
        
        my_rule.prepare(item2, item2);
        for(let i=0; i<this.rules.length; i++) {
          rule = this.rules[i];;
          if(rule.winner() == my_rule.winner()) {
            if(rule.loser() == my_rule.loser()) {
              winner = rule.winner();
            }
          }
        }
        
        if(winner != null && winner != "") {
          return "Player 2 wins";
        }
        
        my_rule.prepare(item2, item1);
        for(let i=0; i<this.rules.length; i++) {
          rule = this.rules[i];;
          if(rule.winner() == my_rule.winner()) {
            if(rule.loser() == my_rule.loser()) {
              winner = rule.winner();
            }
          }
        }
        
        if(winner != null && winner != "") {
          return "Player 2 wins";
        }
        
        return "Draw";
      }
    }
    
    function makeRule(w, l) {
      rule = new Rule();
      rule.prepare(w, l);
      return rule;
    }
    
    function dumbRockPaperScissors(player1, player2) {
      rules = [
        makeRule("Scissors", "Paper"),
        makeRule("Rock", "Scissors"),
        makeRule("Paper", "Rock"),
      ];
      
      let determination = new Determination();
      determination.prepare(rules);
      return determination.winner(player1, player2);
    }
    • function dumbRockPaperScissors(player1, player2) {
    • class Rule {
    • w = '';
    • l = '';
    • prepare=function(w, l) {
    • this.setWinner(w);
    • this.setLoser(l);
    • }
    • setWinner=function(w) {
    • this.w = w;
    • }
    • setLoser=function(l) {
    • this.l = l;
    • }
    • winner=function() {
    • return this.w;
    • }
    • loser=function() {
    • return this.l;
    • }
    • }
    • class Determination {
    • rules = [];
    • prepare=function(rules) {
    • this.rules = rules;
    • }
    • winner=function(item1, item2) {
    • let my_rule = new Rule();
    • my_rule.prepare(item1, item2);
    • let winner = null;
    • for(let i=0; i<this.rules.length; i++) {
    • rule = this.rules[i];;
    • if(rule.winner() == my_rule.winner()) {
    • if(rule.loser() == my_rule.loser()) {
    • winner = rule.winner();
    • }
    • }
    • }
    • if(winner != null && winner != "") {
    • return "Player 1 wins";
    • }
    • my_rule.prepare(item2, item2);
    • for(let i=0; i<this.rules.length; i++) {
    • rule = this.rules[i];;
    • if(rule.winner() == my_rule.winner()) {
    • if(rule.loser() == my_rule.loser()) {
    • winner = rule.winner();
    • }
    • }
    • }
    • if(winner != null && winner != "") {
    • return "Player 2 wins";
    • }
    • my_rule.prepare(item2, item1);
    • for(let i=0; i<this.rules.length; i++) {
    • rule = this.rules[i];;
    • if(rule.winner() == my_rule.winner()) {
    • if(rule.loser() == my_rule.loser()) {
    • winner = rule.winner();
    • }
    • }
    • }
    • if(winner != null && winner != "") {
    • return "Player 2 wins";
    • }
    • return "Draw";
    • }
    • }
    • function makeRule(w, l) {
    • rule = new Rule();
    • rule.prepare(w, l);
    • return rule;
    • }
    • function dumbRockPaperScissors(player1, player2) {
    • rules = [
    • makeRule("Scissors", "Paper"),
    • makeRule("Rock", "Scissors"),
    • makeRule("Paper", "Rock"),
    • ];
    • let determination = new Determination();
    • determination.prepare(rules);
    • return determination.winner(player1, player2);
    • }
Code
Diff
  • const firstNonRepeatingCharacter = (str) => {
      alreadySeen = ""
        for (let i = 0; i < str.length; i++) {
            if(!str.substring(i+1, str.length).includes(str[i]) && !alreadySeen.includes(str[i])){
              return str[i];
            }
          
          alreadySeen += str[i];
        }
        return null; // return null if no unique character is found
    };
    • const firstNonRepeatingCharacter = (str) => {
    • alreadySeen = ""
    • 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];
    • if(!str.substring(i+1, str.length).includes(str[i]) && !alreadySeen.includes(str[i])){
    • return str[i];
    • }
    • alreadySeen += str[i];
    • }
    • return null; // return null if no unique character is found
    • };