Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed functions within mini-challenges #264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/brackets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
23 changes: 22 additions & 1 deletion src/roman-numerals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
20 changes: 19 additions & 1 deletion src/transpose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;