Ad
Code
Diff
  • function dumbRockPaperScissors(player1, player2) {
     
     var draw = characterMatch(player1, player2);
      
        var rock = "Rock";
        var paper = "Paper";
        var scissors = "Scissors";
        var p1Choice = "";
        var p2Choice = "";
    
        for(let i = 0; i< getLetters(player1).length; i++){
          p1Choice = p1Choice + lookUp(i, getLetters(player1));
        }
        for(let i = 0; i< getLetters(player2).length; i++){
          p2Choice = p2Choice + lookUp(i, getLetters(player2));
        }
        if(p1Choice == "Rock" && p2Choice == "Scissors"){
          return "Player 1 wins";
        }
        if(p1Choice == "Rock" && p2Choice == "Paper"){
          return "Player 2 wins";
        }
        if(p1Choice == "Paper" && p2Choice == "Scissors"){
          return "Player 2 wins";
        }
        if(p1Choice == "Paper" && p2Choice == "Rock"){
          return "Player 1 wins";
        }
        if(p1Choice == "Scissors" && p2Choice == "Paper"){
          return "Player 1 wins";
        }
        if(p1Choice == "Scissors" && p2Choice == "Rock"){
          return "Player 2 wins";
        }
      
      if(draw == true){
        return "Draw";
      }
    }
    
    function lookUp(index, array) {
      for (let i = 0; i < array.length; i++) {
        if(index == i){
          return array[i];
        }
      }
    }
    
    function characterMatch(str1, str2){
      var s1letters = [];
      var s2letters = [];
      
      for (let i = 0; i < str1.length; i++) {
        var currentLetter = lookUp(i, str1);
        s1letters.push(lookUp(i, str1));
      }
      
      for (let i = 0; i < str2.length; i++) {
        var currentLetter = lookUp(i, str2);
        s2letters.push(lookUp(i, str2));
      }
      
      var draw = true;
      for(let i = 0; i< s1letters.length; i++){
        for(let j = 0; j < s2letters.length; j++){
          if(i == j){
            if(lookUp(i, s1letters) != lookUp(j, s2letters)){
              draw = false;
            }
          }
        }
      }
      
      return draw;
    }
    
    function getLetters(str){
      var letters = [];
      
      for (let i = 0; i < str.length; i++) {
        var currentLetter = lookUp(i, str);
        letters.push(lookUp(i, str));
      }
      
      return letters;
    }
    • function dumbRockPaperScissors(player1, player2) {
    • var draw = characterMatch(player1, player2);
    • var rock = "Rock";
    • var paper = "Paper";
    • var scissors = "Scissors";
    • var p1Choice = "";
    • var p2Choice = "";
    • for(let i = 0; i< getLetters(player1).length; i++){
    • p1Choice = p1Choice + lookUp(i, getLetters(player1));
    • }
    • for(let i = 0; i< getLetters(player2).length; i++){
    • p2Choice = p2Choice + lookUp(i, getLetters(player2));
    • }
    • if(p1Choice == "Rock" && p2Choice == "Scissors"){
    • return "Player 1 wins";
    • }
    • if(p1Choice == "Rock" && p2Choice == "Paper"){
    • return "Player 2 wins";
    • }
    • if(p1Choice == "Paper" && p2Choice == "Scissors"){
    • return "Player 2 wins";
    • }
    • if(p1Choice == "Paper" && p2Choice == "Rock"){
    • return "Player 1 wins";
    • }
    • if(p1Choice == "Scissors" && p2Choice == "Paper"){
    • return "Player 1 wins";
    • }
    • if(p1Choice == "Scissors" && p2Choice == "Rock"){
    • return "Player 2 wins";
    • }
    • if(draw == true){
    • return "Draw";
    • }
    • }
    • function lookUp(index, array) {
    • for (let i = 0; i < array.length; i++) {
    • if(index == i){
    • return array[i];
    • }
    • }
    • }
    • function characterMatch(str1, str2){
    • var s1letters = [];
    • var s2letters = [];
    • for (let i = 0; i < str1.length; i++) {
    • var currentLetter = lookUp(i, str1);
    • s1letters.push(lookUp(i, str1));
    • }
    • for (let i = 0; i < str2.length; i++) {
    • var currentLetter = lookUp(i, str2);
    • s2letters.push(lookUp(i, str2));
    • }
    • var draw = true;
    • for(let i = 0; i< s1letters.length; i++){
    • for(let j = 0; j < s2letters.length; j++){
    • if(i == j){
    • if(lookUp(i, s1letters) != lookUp(j, s2letters)){
    • draw = false;
    • }
    • }
    • }
    • }
    • return draw;
    • }
    • function getLetters(str){
    • var letters = [];
    • for (let i = 0; i < str.length; i++) {
    • var currentLetter = lookUp(i, str);
    • letters.push(lookUp(i, str));
    • }
    • return letters;
    • }
Code
Diff
  • const firstNonRepeatingCharacter = (str) => {
        for (let i = 0; i < str.length; i++) {
            tempstr = str.slice(0,i) + str.slice(i+1);
            let seenDuplicate = tempstr.includes(str[i]);
            
            if (!seenDuplicate) {
                return str[i];
            }
        }
        return null; // 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;
    • }
    • }
    • tempstr = str.slice(0,i) + str.slice(i+1);
    • let seenDuplicate = tempstr.includes(str[i]);
    • if (!seenDuplicate) {
    • return str[i];
    • }
    • }
    • return null; // return null if no unique character is found
    • };