diff --git a/JustDevRae/Stack/control_Z.js b/JustDevRae/Stack/control_Z.js new file mode 100644 index 0000000..44fba87 --- /dev/null +++ b/JustDevRae/Stack/control_Z.js @@ -0,0 +1,15 @@ +function solution(s) { + var answer = 0; + let Z = 0; + const controlArray = s.split(" "); + for (let i = 0; i < controlArray.length; i++) { + if (controlArray[i] == "Z") { + answer -= Z; + continue; + } + answer += Number(controlArray[i]); + Z = Number(controlArray[i]); + } + + return answer; +} diff --git a/JustDevRae/Stack/crane_claw_game.js b/JustDevRae/Stack/crane_claw_game.js new file mode 100644 index 0000000..ab28cd5 --- /dev/null +++ b/JustDevRae/Stack/crane_claw_game.js @@ -0,0 +1,23 @@ +function solution(board, moves) { + let answer = 0; + const stack = []; + + for (let move of moves) { + for (let i = 0; i < board.length; i++) { + if (board[i][move - 1] !== 0) { + let doll = board[i][move - 1]; + board[i][move - 1] = 0; + + if (stack.length > 0 && stack[stack.length - 1] === doll) { + stack.pop(); + answer += 2; + } else { + stack.push(doll); + } + break; + } + } + } + + return answer; +} diff --git a/JustDevRae/Stack/dart_game.js b/JustDevRae/Stack/dart_game.js new file mode 100644 index 0000000..7b67c10 --- /dev/null +++ b/JustDevRae/Stack/dart_game.js @@ -0,0 +1,41 @@ +function solution(dartResult) { + var answer = 0; + const dartResultArray = dartResult.match(/\d+|[SDT]|\#|\*/g); + const stack = []; + let value; + + for (str of dartResultArray) { + if (str === "S") { + value = stack.pop(); + stack.push(value); + } else if (str === "D") { + value = stack.pop(); + stack.push(value * value); + } else if (str === "T") { + value = stack.pop(); + stack.push(value * value * value); + } else if (str === "*") { + let firtPopValue = stack.pop(); + firtPopValue *= 2; + let secondPopValue = stack.pop(); + if (secondPopValue === undefined) { + stack.push(firtPopValue); + } else { + secondPopValue *= 2; + stack.push(secondPopValue); + stack.push(firtPopValue); + } + } else if (str === "#") { + value = stack.pop(); + stack.push(value * -1); + } else { + stack.push(Number(str)); + } + } + + for (element of stack) { + answer += element; + } + + return answer; +} diff --git a/JustDevRae/Stack/reverse_string.js b/JustDevRae/Stack/reverse_string.js new file mode 100644 index 0000000..003d086 --- /dev/null +++ b/JustDevRae/Stack/reverse_string.js @@ -0,0 +1,10 @@ +function solution(my_string) { + var answer = ""; + const string = [...my_string]; + + for (let i = 0; i < my_string.length; i++) { + answer += string.pop(); + } + + return answer; +} diff --git a/JustDevRae/Stack/valid_parentheses.js b/JustDevRae/Stack/valid_parentheses.js new file mode 100644 index 0000000..14767b2 --- /dev/null +++ b/JustDevRae/Stack/valid_parentheses.js @@ -0,0 +1,16 @@ +function solution(s) { + const stack = []; + for (const c of s) { + if (c === "(") { + stack.push(c); + } else if (c === ")") { + if (stack.length === 0) { + continue; + } else { + stack.pop(); + } + } + } + + return stack.length === 0; +}