DEV Community

Ritik Rana
Ritik Rana

Posted on • Edited on

JavaScript Coding Challenge #1:Learn Split, Splice and Join.

Input : A single string "aaababbcdff"

Output : Reduced string "abacd"

Problem : Delete each pair of adjacent letters that match from the above string.



let s="aaababbcdff";
let arr = s.split("");
for(let i=0;i<arr.length;i++){
  if(arr[i]==arr[i+1]){
    arr.splice(i,2); 
  }
}
s=arr.join("");
console.log(s);

Enter fullscreen mode Exit fullscreen mode

Next Coding Challenge: Add all Integer in a string using JavaScript.

Top comments (2)

Collapse
 
giacomocerquone profile image
Giacomo Cerquone • Edited

pure and lighter :)

const output = str.split("").reduce((acc, el, idx) => acc[acc.length - 1] === el ? acc.slice(0, -1) : acc + el, "");
Collapse
 
giacomocerquone profile image
Giacomo Cerquone

And the code for this challenge is wrong! It's mistaken with the second challenge