Can this alogrithm done without declaritive programing

Can this algorithm be done without slice or any other javaScripts own functions if so than give me a hint by the way i have done it this way.

function  truncateString (str, num) {
  let newStr = '';

  if(str.length > num) {
    newStr = str.slice(0, num) + "..."

  if(str.length <= num ){
    newStr = str


  return newStr;

truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length + 2);

Hello there,

It seems like you have left out information in your post. Which algorithm? How have you done it?

The more information your give us, the more likely we will be able to help.

Of course +1 for Sky’s comment.

But even without seeing the algorithm, the answer will be “yes”. We can point you in the right direction once we have more info.

1 Like

I mean, in theory you could try to write your own “slice” function instead of using the one of JS.
First question as always is: Why would you want to do that? As it means more time to develope and most likely will be not as stable as the already built-in function.

yeah you right sorry now i have updated it .

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

ok sir now i updated the post

OK, to get to your point:

newStr = str.slice(0, num) + "..."

Can this be done without using slice or some other method? Absolutely. Unfortunately, manipulating strings isn’t as simple in JS as it is one some languages (like C, etc.) but you can certainly do it.

I would imagine that in JS, it would involve creating an array of the string - either do that and use a loop to pop off what you don’t need or create an empty array and push on what you do need. Then convert it to a string. Of course, those involve using other prototype methods like push and pop - which for some reason you don’t like. You could also do the second method, slowly concatenating characters onto the string with a loop, but that is messy and can be resource intensive.

But JS is JS - don’t try to turn it into something else. You don’t buy a Ferrari and say you don’t like gasoline so you want to hook a horse up to the front to move it around. If that’s what you wanted, you shouldn’t have bought a car. JS is an amazing language with (as with any other language) strengths and weaknesses - you should be playing to its strengths, don’t try to dwell on its weaknesses.

There are languages where you can do low level manipulations like that - this ain’t it. JS tries to free you up from those things so you can think about the bigger picture. Sure, it comes with some cost, but there are also benefits. Every language makes different trade offs.


Yeah your right but as a beginner i am trying to understand the languages in deep. My point was that slice method is doing something in the background for us right? and i am curious about that thing that slice do and make the work a hell lot easier for us that’s all.

I’m not sure what you mean by ‘in the background’. Slice is pretty explicit about what it does.

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end ( end not included) where start and end represent the index of items in that array. The original array will not be modified.

Slice copies the requested portion of the string or array.

Its a heck of a lot easier than manually copying the portion you need.

Of course i know what slice do i was talking about the alogrithm that slice method uses to copy.

The exact way slice works internally would depend upon the JavaScript engine. But it does not matter exactly how the memory copying semantics work internally. That is not something you want to do manually in JS. And its not really something that you have the ability to do manually in JS.

You can’t, and shouldn’t, write

// C function to do the same
int truncateString(const char *str, int num, char **new_str) {
  // old string length
  size_t str_length;
  for (str_length = 0; str[str_length]; str_length++) {}
  // allocate new string
  size_t new_str_length = str_length;
  if (str_length > num) {
    new_str_length = num + 3;
  *new_str = calloc(new_str_length + 1, sizeof(char));
  // copy old string
  memcpy(*new_str, str, num);
  // add dots if needed
  if (str_length > num) {
    memcpy(&(*new_str[num]), "...", 3);
  return 0;

(Note, this was written off the top of my head without testing. It probably has some compile bugs, but the idea here is that manual string copying is hideous and you don’t want to do it. JS is designed to protect you from manual memory management.)

“How” deep?
To copy a string by hand, you can just use a for-loop. Same for slicing.

However if you want to got deeper… well JS is a high level script-language.
In a computer you got the actual bit-operations done in the CPU, which are determined by the physical construction of the chip. On top of that you got machine-code, assembler and whatever additional programs work inbetween (like the interpreter for JS and interpreters can create some inbetween-code themself…)

And not many people work on those levels because it’s really tedious and weird and frankly high-level programming languages were developed so people don’t need to understand memory-allocation and -adressing by hand.

So you can go VERY deep if you want to. But it’s not recommended unless you are planning on a specific career. I like Machine Learning, so I’ll go with Python and let it do most of the work, without bothering on how I would write a function to create and optimize a million parameters.

string.slice(a, b)? Well a specific range to repeat an action is given → sounds like a for-loop.

More complex operations like regular expression? Search-Algorithms? More? Well…

1 Like

I wouldn’t say it’s that much easier using a build-in method than it is using a for loop and string concatenation (but more convenient and declarative for sure).

I would suggest you try to solve it using something like a for loop and post your code (working or not).

1 Like

I feel I’m missing something. There is nothing that says not to, so why not simply use String.substring(...) DevDocs ?

The result would be identical in this case, yes? I don’t know that it addresses OP’s desire to avoid letting JS manage the memory itself though.

I would use that as well I think, although it’s fairly potato, potahto I guess.

On a completely unrelated note: Was someone from myspace on the standards committee when they came up with String.prototype.blink()? Surely there are more pressing matters than making text blink when it comes to string manipulation?

The end result is identical, yes. Which also speaks to the OP’s question of other ways to do this. I think my question was more toward the OP: is this a question of “in what ways could i do this involving an array?” Or is there a reason you’re not considering string manipulation methods, in particular non-mutating ones?

1 Like

Lol i remember that one. I feel it’s gone the way of the marching ants marquee tag Netscape had blessed us with…

Well, at least the marquee element isn’t a prototype method on the global String object. I mean, really?

Anyway, I digress.

1 Like