Repository navigation
Expand file tree
/
Copy pathhackerRankReverse.js
More file actions
105 lines (87 loc) · 2.97 KB
/
Copy pathhackerRankReverse.js
File metadata and controls
105 lines (87 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// UPER
// U
// REVERSE CHARACTERS IN (POSSIBLY NESTED) STRING THAT ARE IN PARANTHESES
// DON'T REVERSE CHARS NOT IN PARANTHESES
// EX: INPUT foo(bar(baz))blim OUTPUT: foobazrabblim
// ABOVE ACHIEVED BY:
// REVERSE INNERMOST STRING: (baz) returns: foo(barzab)blim
// THEN REVERSE (barzab) RETURNS: foobazrabblim
// EX2: INPUT: foo(bar)baz(blim)boo OUTPUT: foorabbazmibboo
// NOTES
// TEST CASES ARE (TEST) LIKE EXAMPLES AND (((((((TEST)))))))
// 2 (()) CANCELS OUT
// function reverseInParentheses(s) {
// // FIND () IN STRING WITH REG EXP
// var regExp = /\(([^)]+)\)/
// var matches = regExp.exec(s)
// // FIND INNERMOST ()
// // REVERSE CHARS
// // NEXT OUTERMOST ()
// // REVERSE CHARS
// //
// console.log(matches)
// const answer = matches[1]
// console.log(answer)
// const reverseString = answer.split('').reverse().join('')
// console.log(reverseString)
// return reverseString
// }
// reverseInParentheses('(bar)') // OUTPUT rab
// reverseInParentheses(foo(bar(baz))blim) // OUTPUT: foobazrabblim
// reverseInParentheses(foo(bar)baz(blim)boo) // OUTPUT: foorabbazmibboo
// TESTING
// REG EXP ***
// const str2 = 'I expect five hundred dollars ($500).'
// //outpus $500
// var regExp = /\(\$([^)]+)\)/
// console.log(regExp)
var regExp = /\(([^)]+)\)/
var matches = regExp.exec('I expect five hundred dollars (500).')
//matches[1] contains the value between the parentheses
// console.log(matches[1])
// finds position
// var str = 'Visit W3Schools!'
// var n = str.search('W3Schools')
// console.log(n)
// var str = 'Visit (W3S)chools!'
// var n2 = str.search('()')
// console.log(n2)
function reverseInParentheses(s) {
// FIND () IN STRING WITH REG EXP
var firstExp = /^[^()]+/
var regExp = /\(([^)]+)\)/
var innerExp = /\(([^\(\)]+)\)/
var firstPart = firstExp.exec(s)
console.log('firstExp', firstPart)
var matches = regExp.exec(s)
console.log('matches', matches)
var matchesInner = innerExp.exec(s)
console.log('matchesInner', matchesInner)
// FIND INNERMOST ()
console.log('matchesInner', matchesInner)
const answerInner = matches[1]
console.log('answerInner', answerInner)
const firstWord = firstPart
const lastWord = matches[1]
console.log('firstWord', firstWord)
console.log('lastWord', lastWord)
const reverseInnerString = answerInner.split('').reverse().join('')
console.log('reverseInnerString', reverseInnerString)
// REVERSE CHARS
// NEXT OUTERMOST ()
// REVERSE CHARS
//
console.log(matches)
const answer = matches[1]
console.log('answer', answer)
const reverseString = answer.split('').reverse().join('')
console.log('reverseString', reverseString)
// string.concat(value1, value2, ... value_n);
const z = firstWord.concat(firstWord, reverseString)
const r = reverseString.concat(firstWord)
console.log('reverseStringLast', r)
return reverseString
}
// reverseInParentheses('(bar)') // OUTPUT rab
reverseInParentheses('foo(bar)baz') // foorabbaz
// reg exp for inntemost () = /\(([^\(\)]+)\)/