Logo

dev-resources.site

for different kinds of informations.

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

Published at
8/15/2024
Categories
javascript
array
string
trim
Author
ajaymarathe
Categories
4 categories in total
javascript
open
array
open
string
open
trim
open
Author
11 person written this
ajaymarathe
open
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 mode Exit 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 called trimedStr. 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.

string Article's
30 articles in total
Favicon
Leetcode β€” 2942. Find Words Containing Character
Favicon
Python day-28 Dictionary, Frequency of character using nested loops
Favicon
Python Day-19 csv file,String methods,ASCII,Task
Favicon
Redis Cache - A String story
Favicon
Python Day-22 String Functions logic using loops, Recursion, Tasks
Favicon
Greatest Common Divisor of Strings in Javascript
Favicon
Day 22- String Functions and Recursion
Favicon
Day 21- String Functions
Favicon
Python Day-21 String functions logic using loops
Favicon
Python Day-20 String functions logic using loops,Task
Favicon
Day 20 - String functions
Favicon
Day 19 - CSV file, ASCII, String methods
Favicon
Python Day 6- String Functions,Looping-For,ifelse conditions and Task
Favicon
Python Day 5 - String functions
Favicon
Mastering String Operations in JavaScript πŸš€
Favicon
String Data Structures and Algorithms: Essential Interview Questions
Favicon
Find the First Non-Repeated Character in a String
Favicon
Longest substring without repeating characters
Favicon
Create JS function to remove spaces from giving string. ( Using core js and not in-built trim function.)
Favicon
Write a function that removes duplicate characters from a given string. ( Try to write core JS)
Favicon
Knuth Morris Prat algorithm[Pattern Matching]
Favicon
The JS string replace() method
Favicon
Pergunte ao especialista - Strings
Favicon
Strings
Favicon
C# {String Methods}
Favicon
Extending String for Validation in Dart 3
Favicon
String and Trailing comma, get couple and become, Tuple (): A copy & paste mistake to Error and concept
Favicon
Top 10 String Javascript Interview Coding Question
Favicon
Bash string manipulation
Favicon
C++ ηš„ string η‰©δ»Άεˆ°εΊ•δ½”εΉΎε€‹δ½ε…ƒη΅„οΌŸ

Featured ones: