Exclamation marks series #10
Codewars KATA
by trentHarlem
HTML
<p>
Remove the minimum number of exclamation marks from the start/end of each word in the sentence to make their amount equal on both sides.
<br>
Notes:
<br>
<br>
Words are separated with spaces <br>
Each word will include at least 1 letter<br>
There will be no exclamation marks in the middle of a word<br>
<br>
Examples<br>
<br>
remove("Hi!") === "Hi"<br>
remove("!Hi! Hi!") === "!Hi! Hi"<br>
remove("!!Hi! !Hi!!") === "!Hi! !Hi!"<br>
remove("!!!!Hi!! !!!!Hi !Hi!!!") === "!!Hi!! Hi !Hi!"<br>
</p>
CSS
html{
font: 1.1em system-ui;
}
JavaScript
function remove(s) {
let newArr = []
let newS = ''
const arr = s.split(' ')
console.log('arr', arr)
console.log('arr', arr.length)
arr.forEach(word => {
let begin = 0
let middle = ''
let end = 0
let front = ''
let back = ''
output = []
//console.log(word);
for (let i = 0; i < word.length; i++) {
if (word[i] === '!' && middle.length === 0) {
begin++
} else if (word[i] !== '!') {
middle += word[i]
} else if (word[i] === '!') {
end++
}
}
/* console.log('begin', begin)
console.log('middle', middle)
console.log('end', end) */
/* output.push(middle) */
newS += middle
if (begin < end) {
for (let i = 0; i < begin; i++) {
front += '!'
back += "!"
//console.log(newS)
}
/* newS += ' ' */
//console.log(`${front}${middle}${back}`)
(arr.length <= 1) ? newS += '' : newS += ' ';
newS = `${front}${middle}${back}`;
} else if (end < begin) {
for (let i = 0; i < end; i++) {
front += '!'
back += "!"
/* add = '!'
newS = '!'
newS += '!' */
//console.log(output)
//console.log('new string', newS)
}
(arr.length <= 1) ? newS += '': newS += ' ';
newS = `${front}${middle}${back}`;
}
})
return newS
/* newArr.push(output)
console.log(output) */
}
console.log(
//remove("Hi!"), ` === Hi`,
// remove("!Hi! Hi!"), ' === !Hi! Hi',
/* remove("!!Hi!") */
/* remove("!!Hi!!!") */
remove("!!Hi! !Hi!!")
//remove("!!!!Hi!! !!!!Hi !Hi!!!"), '// === !!Hi!! Hi !Hi!'
)
/* begin = word.split('!').length
console.log(begin) */
/*
function count(char) {
(char === '!') ? begin++ : begin;
console.log('begin',begin)
} */