Skip to content
This repository was archived by the owner on Jun 6, 2018. It is now read-only.

Latest commit

 

History

History
47 lines (34 loc) · 1.31 KB

File metadata and controls

47 lines (34 loc) · 1.31 KB

Ðåàëèçóéòå ôóíêöèþ isBalanced2(). Îíà ïîõîæà íà ôóíêöèþ isBalanced() èç ïðåäûäóùåé ãðóïïû çàäàíèé, íî ïîääåðæèâàåò òðè òèïà ñêîáîê: ôèãóðíûå {}, êâàäðàòíûå [], è êðóãëûå (). Ïðè ïåðåäà÷å ôóíêöèè ñòðîêè, â êîòîðîé èìåþòñÿ íåïðàâèëüíûå ñêîáî÷íûå ïîñëåäîâàòåëüíîñòè, ôóíêöèÿ äîëæíà âîçâðàùàòü false.

isBalanced2('(foo { bar (baz) [boo] })') // true isBalanced2('foo { bar { baz }') // false isBalanced2('foo { (bar [baz] } )') // false

alert(isBalanced2('(foo { bar (baz) [boo] })') )// true alert(isBalanced2('foo { bar { baz }') ) // false alert(isBalanced2('foo { (bar [baz] } )') ) // false

function isBalanced2(str){ let res = []; for (const char of str){ if (['{', '}', '[', ']', '(', ')'].indexOf(char) !== -1){ res.push(char); } }

if (['{', '[', '('].indexOf(res[0]) === -1){ return false } if (res.length%2===1){ return false }

for (let i = 0; i<res.length; i++){

let indexI = ['{', '[', '('].indexOf(res[i]);

if ( indexI !== -1){

	let right = 0;
  
	for (let k = i+1; k<res.length; k += 2){
  
  	//let indexK = ['}', ']', ')'].indexOf(res[k]);
    //let rightlist = 
  	if (res[k] === ['}', ']', ')'][indexI]){
    	right++;
    }
    
  }
  if (right === 0){
  	return false;
  }
}

} return true }