repeatChar in Node.js

Background

Let us continue with our task of seeing how repeatChar is coded in various languages.

In this post, we will use JavaScript.

Specifically, NodeJS.

 

Code

Outline

  1. Function:-repeatChar ( char ch, int numberofChars)
    • Declare Variables
      • buffer as an array ( var buffer = [] )
    • Validate Arguments
      • ch
        • Check if ch is a string
      • numberofChars
        • Check if numberofChars is a number
    • Utilize Loop
      • Set buffer’s character on each array position to argument ch
    • Issue Join Method against character array; separating array characters with empty character
      • .join(”)

Script

 

/*
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 ) ;
view raw repeatChar.js hosted with ❤ by GitHub

Coding Tools

Online Compiler

Here are the online tools that we used to code the script:-

  1. TutorialsPoint ( https://www.tutorialspoint.com/ )
    • CodingGrounds
  2. Coding Rooms

Source Code Control

repeatChars

GitLab

  1. Series
    Link
  2. NodeJS
    Link

GitHub

  1. NodeJS
    Link

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s