DEV Community

Ritik Rana
Ritik Rana

Posted on • Edited on

1 1

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay