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

submit solution #22

Open
wants to merge 2 commits into
base: challenge-well-formed-strings
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class StringValidator{
constructor(){
this.pairs = { "[": "]", "{": "}" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My first JavaScript solution! 👏

}
// loop through array
// get first pair
// see if second exists
// if does exit
// else return false

validate(string){
var stringArray = string.split("");
var answer;
var bcount = 0;
var hcount = 0;
var pcount = 0;
var falseAnswer;

for(var i = 0; i < stringArray.length; i++){
var current = stringArray[i];
if(current == "("){ pcount += 1; }
if(current == "["){ bcount += 1; }
if(current == "{"){ hcount += 1; }

for(var j = 0 + 1; j < stringArray.length; j++){
var newItem = stringArray[j];
if(current == "(" && newItem == ")"){ pcount += 1; }
if(current == "[" && newItem == "]"){ bcount += 1; }
if(current == "{" && newItem == "}"){ hcount += 1; }
}
}

if((pcount % 2 != 0) || (hcount % 2 != 0) || (bcount % 2 != 0)){
answer = false;
} else {
answer = true;
}
console.log(answer);
}
}

module.exports = StringValidator;

var v = new StringValidator();
v.validate("([)]")