Example, input:
aasddeeeaa
Result:
AaSDdEeeAa
I split the string into an array of letters, then have to check if the letter is occurred again after
CodePudding user response:
this way
const Cap1stR = s => [...s].reduce((r,c,i,{[i-1]:p})=>(r =(p===c)?c:c.toUpperCase(),r),'')
console.log( Cap1stR('aasddeeeaa') )
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
Here is a lazy solution to your problem (It works but it is ugly as hell).
let oldString = "aasddeeeaa"
let newString = ""
for(let charNb =1;charNb < oldString.length-1; charNb ){
let char = oldString[charNb]
let previousChar = oldString[charNb-1]
if(char === previousChar){
newString = char
}
if(char !== previousChar){
newString = char.toUpperCase()
}
}
console.log(newString)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
You should use regex for this kind of problem but this will probably be good enough. I hope this is what you needed.
CodePudding user response:
const values = "aasddeeeaa"
let str = ""
let letter = 0;
for (let data of values.split("")) {
if (letter == 0) {
str = data.toUpperCase();
} else {
let prev = values.split("")[letter - 1];
if (data.toLowerCase() != prev.toLowerCase()) {
str = data.toUpperCase()
} else {
str = data.toLowerCase()
}
}
letter ;
}
console.log("Answere =>", str)
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>