Javascript Algorithms and Data Structures Challenge: Day 8

  1. Convert into array of chars
  2. Reverse
  3. Remove 1st
  4. Convert back
let s="Hello, wold";String.prototype.reverse= function () {
return this.split('').reverse().join('');

}
String.prototype.removeFirst=function (c){
return this.replace(c,'');
}
s=s.reverse();
s=s.removeFirst('l');
s=s.reverse();
console.log(s)
//"Hello, wod"
  1. Convert to array
  2. Remove
  3. Rejoin
let s="Hello, wolld";function removeLast(str, c){
const arr=s.split(c);

const newString=arr.reduce((a, v, i, r)=>{
if( i === (r.length-1)){
return a+v;
}

if( i === (r.length-2)){
return a+v;
}

return a+v+c;

},'')
return newString;}console.log(removeLast(s, 'l'));
  1. splice method
  2. join
let s="Hello, wolld";function removeLast(str, c){
let arr=s.split('');
let lastIndex, newArr, newStr;

for(let i=0; i<arr.length;i++){
let tmp=arr[i];
if(tmp===c){
lastIndex=i;
}

}


if(lastIndex){
arr.splice(lastIndex, 1);
newStr=arr.join('');
}
return newStr;}console.log(removeLast(s, 'l'))

coder, founder and explorer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Real time Chat application with Socket.io in NodeJS

The IIFE explained

⛅️ AWS Products and Components ⛅️

Aspect-oriented Programming

This is a hackathon type design

New React Native Architecture- Value Offerings and Improvements in 2020!

Avoid These Two Types of Frameworks

Things I Did This Week => React Redux

Yes it's a Confusing Operator 🤔

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
FRANK GO

FRANK GO

coder, founder and explorer.

More from Medium

React JS and its Advantages: A Quick Guide

What are the components’ lifecycle methods in ReactJS?

Es6 Interview for Beginner

React Basics — What goes in constructor?