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
- 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
- ch
- 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(”)
- Declare Variables
Script
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 ) ; |
Coding Tools
Online Compiler
Here are the online tools that we used to code the script:-
- TutorialsPoint ( https://www.tutorialspoint.com/ )
- CodingGrounds
- Coding Rooms
- JavaScript ( https://www.codingrooms.com/compiler/javascript )
Source Code Control
repeatChars
GitLab
GitHub
- NodeJS
Link