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

my mini-challenge #272

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
48 changes: 24 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion src/brackets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@
* @param {string} str The string of brackets.
* @returns {"valid" | "invalid"} Whether or not the string is valid.
*/
function isValid(str) {}
function isValid(str) {
let arrBracket = [];

for (let i in str) {
if (str[i] == '(' || str[i] == '[' || str[i] == '{') {
arrBracket.push(str[i]);
} else {
let braInd = arrBracket.length - 1;

if (str[i] == ')' && arrBracket[braInd] == '(') {
arrBracket.pop();
} else if (str[i] == ']' && arrBracket[braInd] == '[') {
arrBracket.pop();
} else if (str[i] == '}' && arrBracket[braInd] == '{') {
arrBracket.pop();
} else {
return 'invalid';
}
}
}
return arrBracket.length == 0 ? 'valid' : 'invalid';
}

module.exports = isValid;
25 changes: 24 additions & 1 deletion src/roman-numerals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@
* @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 values = {
I : 1,
V : 5,
X : 10,
L : 50,
C : 100,
D : 500,
M : 1000,
}
let result = 0;
let previousChar = 0;
for (let i = roman.length -1; i >= 0; i--){
const currentChar = values[roman[i]];
if (currentChar < previousChar){
result -= currentChar;
} else {
result += currentChar;
}
previousChar = currentChar
}
return result

}

module.exports = romanToDecimal;
11 changes: 10 additions & 1 deletion src/transpose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
* @param {number[]} array The array to transpose
* @returns {number[]} The transposed array
*/
function transpose(array) {}
function transpose(array) {
let transposedArr = [];
for (let i=0; i < array[0].length; i++){
transposedArr[i] = [];
for (let j=0; j < array.length; j++){
transposedArr[i][j] = array[j][i]
}
}
return transposedArr
}

module.exports = transpose;