Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,288 members, 7,807,977 topics. Date: Thursday, 25 April 2024 at 12:57 AM

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

Nairaland Forum / Science/Technology / Programming / Extraction In Javascript: How Would You Write Your Own Code? (456 Views)

Javascript:how Do I Use drawBox Function.. / Can You Write Codes With A Tablet Instead Of A Laptop? / How To Retrieve Data From An Api Using Fetch Api In Javascript (2) (3) (4)

(1) (Reply) (Go Down)

Extraction In Javascript: How Would You Write Your Own Code? by thanksjosh005: 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: 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();

1 Like

Re: Extraction In Javascript: How Would You Write Your Own Code? by Nobody: 2:57pm On Jun 10, 2022
Shortest and fastest way posible

1 Like

Re: Extraction In Javascript: How Would You Write Your Own Code? by thanksjosh005: 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: 3:35pm On Jun 10, 2022
GREATIGBOMAN:
Shortest and fastest way posible

Lol... I agree

(1) (Reply)

I'm Looking For Web Developers To Work With Me / Download: Eloquent Javascript PDF (FREE) / Web Development For Beginners

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 9
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.