function dumbRockPaperScissors(player1, player2) { let answers = [player1, player2]; let winners = [ ['Rock', 'Scissors'], ['Scissors', 'Paper'], ['Paper', 'Rock'] ] let wouldBeat = []; for (let a = 0; a < answers.length; a++) { for (let w = 0; w < winners.length; w++) { if (answers[a] == winners[w][0]) { wouldBeat.push(winners[w][1]) } } } if (answers[1] == wouldBeat[0]) { return 'Player 1 wins'; } if (answers[0] == wouldBeat[1]) { return 'Player 2 wins'; } return 'Draw'; }
- function dumbRockPaperScissors(player1, player2) {
- let answers = [player1, player2];
- let winners = [
- ['Rock', 'Scissors'],
- ['Scissors', 'Paper'],
- ['Paper', 'Rock']
- ]
- let wouldBeat = [];
- for (let a = 0; a < answers.length; a++) {
- for (let w = 0; w < winners.length; w++) {
- if (answers[a] == winners[w][0]) {
- wouldBeat.push(winners[w][1])
- }
- }
- }
- if (answers[1] == wouldBeat[0]) {
- return 'Player 1 wins';
- }
- if (answers[0] == wouldBeat[1]) {
- return 'Player 2 wins';
- }
- return 'Draw';
- }
const firstNonRepeatingCharacter = (str) => { if (str == '') return null; let charMap = {}; for (let i = 0; i < str.length; i++) { let thisChar = str[i]; if (!charMap[thisChar]) { charMap[thisChar] = 1; } else { charMap[thisChar] = charMap[thisChar] + 1; } } for (let i = 0; i < str.length; i++) { let thisChar = str[i]; if (charMap[thisChar] == 1) { return thisChar; } } return null; };
- const firstNonRepeatingCharacter = (str) => {
- if (str == '') return null;
- let charMap = {};
- 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];}- let thisChar = str[i];
- if (!charMap[thisChar]) {
- charMap[thisChar] = 1;
- } else {
- charMap[thisChar] = charMap[thisChar] + 1;
- }
- }
return null; // return null if no unique character is found- for (let i = 0; i < str.length; i++) {
- let thisChar = str[i];
- if (charMap[thisChar] == 1) {
- return thisChar;
- }
- }
- return null;
- };