Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Ajay Marathe
Ajay Marathe

Posted on

Create JS function to remove spaces from giving string. ( Using core js and not in-built trim function.)

const trim = (string) => {    let strArr = string.split("");    let trimedStr = [];    strArr.forEach((item) => {      if (item !== " ") {        trimedStr.push(item);      }    });    return trimedStr.join("");  };  console.log("trim", trim("Hello world nice world")); // output => trim: Helloworldniceworld
Enter fullscreen modeExit fullscreen mode

Problem Explanation

Let's break down the problem in simple terms:

You have a piece of code that defines a function called trim. The purpose of this function is to remove all the spaces from a given string. In other words, if you pass a sentence with spaces into this function, it will return the same sentence but with all the spaces removed.

How the Function Works:

  1. Splitting the String: The function starts by taking the input string(e.g., "Hello world nice world") and splits it into an array of individual characters. For example, "Hello world" becomes['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']...

  2. Filtering Out Spaces: The function then goes through each character in the array. If the character is not a space(' '), it adds it to a new array calledtrimedStr. If it is a space, it simply skips it.

  3. Rejoining the Characters: After filtering out the spaces, the function takes the remaining characters and joins them back together into a single string without any spaces.

  4. Returning the Result: Finally, the function returns the new string that has no spaces.

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jonrandy profile image
Jon Randy 🎖️
🤖 Artisanal developer - coding with varying degrees of success since 1983
  • Location
    Bangkok 🇹🇭
  • Joined
• Edited on• Edited

Filtering out spaces is not whattrim does. It will remove leading and trailingwhitespace.

There is also an issue with the way you're splitting the string. Try your code with a string containing emojis - it will break. A better, although still not perfect version of your code would be:

consttrim=string=>{lettrimmedStr='';[...string].forEach(char=>trimmedStr+=char!=''?char:'')returntrimmedStr}console.log("trim",trim("Hello world nice world"))// output => trim: Helloworldniceworldconsole.log("trim",trim("Hello world nice 🙂world"))// output => trim: Helloworldnice🙂world
Enter fullscreen modeExit fullscreen mode
CollapseExpand
 
ajaymarathe profile image
Ajay Marathe
In Love with React, Javascript & Node.js 😍😍
  • Location
    Pune, India
  • Joined

Thanks for the explanation, appreciated your response 🙂

CollapseExpand
 
jonrandy profile image
Jon Randy 🎖️
🤖 Artisanal developer - coding with varying degrees of success since 1983
  • Location
    Bangkok 🇹🇭
  • Joined
• Edited on• Edited

A better route overall is probably usingreplace on the string though.

Thread Thread
 
ajaymarathe profile image
Ajay Marathe
In Love with React, Javascript & Node.js 😍😍
  • Location
    Pune, India
  • Joined

Cool, got it, thanks, keep looking my other posts and future post too, and do suggest the different approaches 🙂✌️

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

In Love with React, Javascript & Node.js 😍😍
  • Location
    Pune, India
  • Joined

More fromAjay Marathe

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp