diff --git a/src/brackets/index.js b/src/brackets/index.js index 96b6766..800f8ad 100644 --- a/src/brackets/index.js +++ b/src/brackets/index.js @@ -4,6 +4,41 @@ * @param {string} str The string of brackets. * @returns {"valid" | "invalid"} Whether or not the string is valid. */ -function isValid(str) {} +function isValid(str) { + + const count = []; + + // create a correct pairing with key:value pairs + const correctPairing = { + '(': ')', + '[': ']', + '{': '}', + }; + + for (let char of str) { + + // iterate through the characters in the string + if (char in correctPairing) { + + // push consecutive opening brackets to the count variable + + count.push(char); + } else if (Object.values(correctPairing).includes(char)) { + + // remove most current occurence of opening brackets (key) if a closing bracket (value) is met + + const lastOpeningBracket = count.pop(); + + // if the popped value does not match the key, return invalid; + + if (!lastOpeningBracket || correctPairing[lastOpeningBracket] !== char) { + return "invalid"; + } + } + } + // check the length of remaining characters in the count variable, if 0, all brackets have succesfully paired, else it is invalid + + return count.length === 0 ? "valid" : "invalid"; +} module.exports = isValid; diff --git a/src/roman-numerals/index.js b/src/roman-numerals/index.js index 38afb19..205af7c 100644 --- a/src/roman-numerals/index.js +++ b/src/roman-numerals/index.js @@ -4,6 +4,27 @@ * @param {string} roman The all-caps Roman numeral between 1 and 3999 (inclusive). * @returns {number} The decimal equivalent. */ -function romanToDecimal(roman) {} +function romanToDecimal(roman) { + const romanToDecimal = { + I: 1, + V: 5, + X: 10, + L: 50, + C: 100, + D: 500, + M: 1000, + }; + + let total = 0; + + for (let i = 0; i < roman.length; i++) { + const currentChar = romanToDecimal[roman[i]]; + const nextChar = romanToDecimal[roman[i + 1]]; + + total += nextChar && nextChar > currentChar ? -currentChar : currentChar; + } + + return total; +} module.exports = romanToDecimal; diff --git a/src/transpose/index.js b/src/transpose/index.js index adec201..b38973e 100644 --- a/src/transpose/index.js +++ b/src/transpose/index.js @@ -4,6 +4,24 @@ * @param {number[]} array The array to transpose * @returns {number[]} The transposed array */ -function transpose(array) {} +function transpose(array) { + + // create new Parent and Child Arrays + const newRowLength = array.length; + const newColLength = array[0].length; + + const transposedArray = []; + + // iterate over array to create new rows and column + for (let col = 0; col < newColLength; col++) { + const newRow = []; + for (let row = 0; row < newRowLength; row++) { + newRow.push(array[row][col]); + } + transposedArray.push(newRow); + } + + return transposedArray; +} module.exports = transpose;