₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,239 members, 8,420,927 topics. Date: Friday, 05 June 2026 at 02:19 PM

Toggle theme

Extraction In Javascript: How Would You Write Your Own Code? - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingExtraction In Javascript: How Would You Write Your Own Code? (649 Views)

1 Reply (Go Down)

Extraction In Javascript: How Would You Write Your Own Code? by thanksjosh005(op): 12:06pm On Jun 10, 2022
Hello house....

What are your takes on this? Probably, which of these algos would you use when writing codes or do you have your own method as well?

Method 1 implies a more technical use of the JavaScript slice() method while Method 2 looks much like a simpler method for extracting a specified amount of string.



Method 1
truncate_string = function (str1, length) {

if ((str1.constructor === String) && (length>0)) {
return str1.slice(0, length);
}
};
console.log(truncate_string("Robin Singh",4));

//outputs Robi





Method 2
let text = "Hello world!"; 

let result = text.slice(3);

console.log(result);

//outputs lo world!






Reference:
JavaScript: Extract A Specific Number of Characters from a String
url: https://www.w3resource.com/javascript-exercises/javascript-string-exercise-4.php

JavaScript String slice()
url:https://www.w3schools.com/jsref/jsref_slice_string.asp
Re: Extraction In Javascript: How Would You Write Your Own Code? by thanksjosh005(op): 12:56pm On Jun 10, 2022
Method 1 is very succinct and clearly self-explained. Writing such algo could be based off your techne or mastery with writing codes. Method 1 can be broken down into concepts in JavaScript like
• JS object
three ways objects can be written:
via
° string literal
° the new keyword
° object constructor

Here the object constructor type was used which always include executing the code with the JS function keyword

• JS operators
comparison and tenary operators were used

• JS if/else

• JS slice() method

A practical understanding of these can help you understand the technicality of Method 1


JS is extensive and consists of various ways you can write code & build algos. Because we likewise have several existing versions of JS, familiarity with JS versions could also help you write various methods for your algos too. smiley
Re: Extraction In Javascript: How Would You Write Your Own Code? by Deicide: 2:44pm On Jun 10, 2022

foo = () => {};
foo();
Re: Extraction In Javascript: How Would You Write Your Own Code? by Nobody: 2:57pm On Jun 10, 2022
Shortest and fastest way posible
Re: Extraction In Javascript: How Would You Write Your Own Code? by thanksjosh005(op): 3:33pm On Jun 10, 2022
Deicide:

foo = () => {};
foo();
Of course! Thanks for the addition.
Re: Extraction In Javascript: How Would You Write Your Own Code? by thanksjosh005(op): 3:35pm On Jun 10, 2022
GREATIGBOMAN:
Shortest and fastest way posible
Lol... I agree
1 Reply

Javascript:how Do I Use drawBox Function..Can You Write Codes With A Tablet Instead Of A Laptop?How Do I Convert Object To Arrey In Javascript234

QR Code Monkey - Free Qr Code GeneratorA Nairalander Creates A Music App Using React And Django RESTWhy Is This Try Catch Not Working