Last active
May 15, 2022 02:49
-
-
Save DanielAdeniji/75a6ae7417d7d2b783e767049aad28b1 to your computer and use it in GitHub Desktop.
repeat Character in Node JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Function:- repeatChar | |
*/ | |
function repeatChar(ch, count) | |
{ | |
/* | |
Declare buffer as an array | |
*/ | |
var buffer = []; | |
var str = ""; | |
var i = 0; | |
var log = ""; | |
const parameterTypeChExpected = "string"; | |
const parameterTypeCountExpected = "number"; | |
/* | |
Check type of parameter ch | |
*/ | |
if ( typeof(ch) != parameterTypeChExpected) | |
{ | |
log = "Parameter ch is expected to be of type " + parameterTypeChExpected; | |
log = log + ". " + "Unfortunately ch is of type " + typeof(ch); | |
throw new Error(log); | |
} | |
/* | |
Check type of parameter count | |
*/ | |
if ( typeof(count) != parameterTypeCountExpected) | |
{ | |
log = "Parameter count is expected to be of type " + parameterTypeCountExpected; | |
log = log + ". " + "Unfortunately count is of type " + typeof(count); | |
throw new Error(log); | |
} | |
/* | |
Start from 0 to count-1 | |
Incrementing by 1 | |
*/ | |
for(i=0; i < count;i++) | |
{ | |
buffer[i] = ch; | |
} | |
/* | |
Join each array element separating by empty string | |
*/ | |
str = buffer.join(''); | |
return (str); | |
} | |
/* | |
Function:- repeatCharUsingFill | |
*/ | |
function repeatCharUsingFill(ch, count) | |
{ | |
/* | |
Declare buffer as an array | |
*/ | |
var buffer = []; | |
var i = 0; | |
var log = ""; | |
const parameterTypeChExpected = "string"; | |
const parameterTypeCountExpected = "number"; | |
/* | |
Check type of parameter ch | |
*/ | |
if ( typeof(ch) != parameterTypeChExpected) | |
{ | |
log = "Parameter ch is expected to be of type " + parameterTypeChExpected; | |
log = log + ". " + "Unfortunately ch is of type " + typeof(ch); | |
throw new Error(log); | |
} | |
/* | |
Check type of parameter count | |
*/ | |
if ( typeof(count) != parameterTypeCountExpected) | |
{ | |
log = "Parameter count is expected to be of type " + parameterTypeCountExpected; | |
log = log + ". " + "Unfortunately count is of type " + typeof(count); | |
throw new Error(log); | |
} | |
/* | |
Allocate memory | |
*/ | |
buffer = new Array(count); | |
/* | |
Check allocated object | |
*/ | |
if ( (buffer === undefined || buffer === null) ) | |
{ | |
log = "Was not able to allocate memory in repeatCharUsingFill"; | |
log = log + ". " + "requested memory of byte size " + count; | |
throw new Error(log); | |
} | |
/* | |
Set buffer to char ( ch ) | |
*/ | |
buffer.fill(ch); | |
/* | |
Join each array element separating by empty string | |
*/ | |
str = buffer.join(''); | |
return (str); | |
} | |
/* | |
Get the character | |
*/ | |
const chSeparator = '='; | |
/* | |
Get phrase | |
*/ | |
phrase = 'Head of the class'; | |
/* | |
Get the length of the phrase | |
*/ | |
len = phrase.length; | |
/* | |
Repeat Character | |
*/ | |
filler = repeatChar( chSeparator, len); | |
/* | |
Repeat Character Using Fill Method | |
*/ | |
filler2 = repeatCharUsingFill(chSeparator, len); | |
/* | |
Display Phrase | |
*/ | |
console.log( phrase ); | |
/* | |
Display Filler | |
*/ | |
console.log ( filler ) ; | |
console.log ( filler2 ) ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment