Ad
Code
Diff
  • function dumbRockPaperScissors(player1, player2) {
        let size1 = 0;
        for (let i = 0; i < player1.length; i++) {
            for (let j = 0; j < player1.length; j++) {
                if (i === j) {
                    size1++;
                }
            }
        }
    
        let size2 = 0;
        for (let i = 0; i < player2.length; i++) {
            for (let j = 0; j < player2.length; j++) {
                if (i === j) {
                    size2++;
                }
            }
        }
    
        if (size1 === size2) {
            return 'Draw';
        }
    
        if (size1 > size2) {
            if (
                (player1 === 'Rock' && player2 === 'Scissors') ||
                (player1 === 'Scissors' && player2 === 'Paper') ||
                (player1 === 'Paper' && player2 === 'Rock')
            ) {
                return 'Player 1 wins';
            } else {
                return 'Player 2 wins';
            }
        } else {
            if (
                (player2 === 'Rock' && player1 === 'Scissors') ||
                (player2 === 'Scissors' && player1 === 'Paper') ||
                (player2 === 'Paper' && player1 === 'Rock')
            ) {
                return 'Player 2 wins';
            } else {
                return 'Player 1 wins';
            }
        }
    }
    
    • function dumbRockPaperScissors(player1, player2) {
    • let size1 = 0;
    • for (let i = 0; i < player1.length; i++) {
    • for (let j = 0; j < player1.length; j++) {
    • if (i === j) {
    • size1++;
    • }
    • }
    • }
    • }
    • let size2 = 0;
    • for (let i = 0; i < player2.length; i++) {
    • for (let j = 0; j < player2.length; j++) {
    • if (i === j) {
    • size2++;
    • }
    • }
    • }
    • if (size1 === size2) {
    • return 'Draw';
    • }
    • if (size1 > size2) {
    • if (
    • (player1 === 'Rock' && player2 === 'Scissors') ||
    • (player1 === 'Scissors' && player2 === 'Paper') ||
    • (player1 === 'Paper' && player2 === 'Rock')
    • ) {
    • return 'Player 1 wins';
    • } else {
    • return 'Player 2 wins';
    • }
    • } else {
    • if (
    • (player2 === 'Rock' && player1 === 'Scissors') ||
    • (player2 === 'Scissors' && player1 === 'Paper') ||
    • (player2 === 'Paper' && player1 === 'Rock')
    • ) {
    • return 'Player 2 wins';
    • } else {
    • return 'Player 1 wins';
    • }
    • }
    • }
Code
Diff
  • const firstNonRepeatingCharacter = (str) => {
        const charCounts = new Map();
    
        for (let i = 0; i < str.length; i++) {
            charCounts.set(str[i], (charCounts.get(str[i]) || 0) + 1);
        }
    
        for (let i = 0; i < str.length; i++) {
            if (charCounts.get(str[i]) === 1) {
                return str[i];
            }
        }
    
        return null;
    };
    
    // def first_non_repeating_character(str)
    //   char_counts = Hash.new(0)
    
    //   str.each_char { |char| char_counts[char] += 1 }
    
    //   str.each_char do |char|
    //     return char if char_counts[char] == 1
    //   end
    
    //   nil
    // end
    • const firstNonRepeatingCharacter = (str) => {
    • const charCounts = new Map();
    • 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) {
    • charCounts.set(str[i], (charCounts.get(str[i]) || 0) + 1);
    • }
    • for (let i = 0; i < str.length; i++) {
    • if (charCounts.get(str[i]) === 1) {
    • return str[i];
    • }
    • }
    • return null; // return null if no unique character is found
    • };
    • return null;
    • };
    • // def first_non_repeating_character(str)
    • // char_counts = Hash.new(0)
    • // str.each_char { |char| char_counts[char] += 1 }
    • // str.each_char do |char|
    • // return char if char_counts[char] == 1
    • // end
    • // nil
    • // end