Posts

String repeat in JavaScript

String repeat is a function that can be used to multiply a string for a given number of times, its a feature that is closely related to string multiplication operator in Python programming language, as compared to Python's syntax;
("String") * (number of times to repeat) "A" * 3
// returns "AAA"
Something similar may be archived using string repeat function in JavaScript as;
("String").repeat(number Of times to repeat)
For example to triple "A" you can do as;
"A".repeat(3)
Practical example is with lyrics;
const part = "i feel you, "
const lyrics = part.repeat(4) + " A, B, C" // "i feel you, i feel you, i feel you, i feel you, A, B, C"
Further reading;
String.prototype.repeat() MDN

Lambadas in kotlin programming language

Lambadas are similar to arrow functions in JavaScript they are a short alternative method to declare functions in kotlin programing language, You can create a function expression as;
val double = {num: Int -> num * 2} val cube = { num: Int -> num * num * num} val square = {num: Int ->
    val product = num * num
    product }
The last expression in a lambada is considered as a return value; Parameters can be omitted, for a single parameter lambada the value of the omitted parameter is in the variable 'it'

Rest operator in JavaScript

Rest operator is similar to spread operator its an es6 feature that allows converting arguments to an array of values;

Syntax of spread operator is as follows;

function spr(...params) { console.log(params) } spr(1,2,3) // [1, 2, 3] You can use rest operator among other arguments

function sprr(args, ...params) { console.log(params) } spr('foo', 1, 2, 3) // [1, 2, 3] More information:
Restparameters MDN

JavaScript String includes function

String includes is a function introduced to JavaScript in Escmascript 'es6' that returns a boolean true , or false based on the evaluation whether or not a string contains the string passed as an argument to the function For example;


const str = "John swana" str.includes('John') // true str.includes('duke') // false Running string includes testing whether 'John swana' includes John returns trues as it's a fact.

Running the same string against duke returns false duke is not part of the string 'str'

comparison on var, let and const variable declaration

Introduction
let, const and var are keywords used for variable declarations in JavaScript programming language,const and let were newly introduced to the programming language in Escmascript es6 or es2016.Declaration keywords
the syntax of declaring a variable using let keyword is done as;let {variable name} = valuefor const it's done as;const {variable name} = valuesimilarly with var;var {variable name} = valueScope for 'let' and 'const' variables
let and const declared variables are block scoped that means values assigned remain within the block of assignment;
take for instance;if(true) {
   const declaration = "declared"
}console.log(typeof declaration)
// undefinedvalues remain accessible only within the if blockScope for 'var' variables
var declared variables are function scoped so variables declared within a function are only accessible within the function it's self for example;function assign(param){
    var assignment = param;
}assign(&…

Es6 destructuring operator in JavaScript

JavaScript from es6 enables destructuring operation,
In JavaScript destructuring is done using the syntax;
const [elements] = [array] const {properties} = {object}
For instance using Math object as an example destructuring can be done as follows;
Object matching syntax;
const {abs, min, max} = Math;
List matching syntax;
const [max, min, avg] = [10, 1, 5]
Fail-soft destructuring similar to object property look up returns undefined if there's no property matching;
var [item] = [];
item === undefined;
Fail-soft destructuring can also be done with predefined default vslues as follows;
var [item = 1] = [];
item === 1;
More information: MDNDestructuringassignment

How to write a self invoked function in JavaScript

Self invoked functions are function that run on themselves without need to be called, the syntax to create self invoked functions is as follows;

(function(){ // function body })() the runtime will run everything in a self invoked function's body You can pass parameters to the function using the syntax below;
(function(params){     // function body })(params) parameters will be made available to the function as any other argument

JavaScript default parameters

Image
Since es6 its possible to include default function parameters just like other programing languages such as php do, default parameters cut down the need to initially set default Parameters within the code as;
const vec function (x, y){ if(typeof x == "undefined") x = 0; if(typeof y == "undefined") y = 0; return { x, y } }
now the above can be done in a more simple way , using default parameters as;
const vec function (x = 0, y = 0){ return { x, y } } In either of the functions if you omit either of the Parameters your parameter will default to zero ie 0;

How to get first and last character of a string using 'String.startsWith' and 'String.endsWith' in JavaScript

Image
String starts with is an emcscript es6 feature that allows JavaScript developers to test a string if the first character of a string matches the given criteria ,

Take for instance you have a string, you want to test if it starts with characters '#'
const str = "#JavaScript" cons strr = "I love #kotlin" str.startWith('#') // true strr.endsWith('#kotlin') // true strr.startWith('#') // false str.endsWith('#kotlin') // false Both functions return a boolean true for a match and false if there's no match.

endsWith does what startsWith does except that it does it at the end of the string

How to make a static http server in nodejs using express

Image
Express is a fast unopinionated, minimalist web framework for nodejs Authored by  TJ Holowaychuk
Express is a framework for building robust http servers, single page applications and API's among others.

How to create an http server in express

Open your text editor and terminal,
run the commands below to create a project directory, application file
and a landing page

mkdir project
cd project
mkdir static
echo "Hello World" > static index.html
touch app.js

You must have a directory structure as follows; |Project/
    |static/
        |index.html
    |app.js

Project directory is the root directory of the application Static directory is where static files, webpages will be stored.

index.html is an index page with text "Hello world".

app.js is a blank JavaScript file, open your editor and paste the following few lines of code into app.js


import express from 'express' const app = express() const port = 3000 app.use('/', express.static('static…