Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions JavaScript/reverseString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// REVERSE STRING ALGORITHM
//
// Create a function that takes in a string (str) as an argument,
// reverses it and returns the reversed string
//
// ----------------------------------------------------------------
//
// Test Cases:
//
// The string 'Hello World' passed as the argument to the function
// returns a string.
//
// The string 'Hello World' passed as the argument to the function
// returns 'dlroW olleH'
//
// ----------------------------------------------------------------

function reverseString(str){
return str.split('').reverse().join('');
}

reverseString('Hello World')
// returns 'dlroW olleH'
30 changes: 30 additions & 0 deletions JavaScript/truncateString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// TRUNCATE STRING ALGORITHM
//
// Create a function that takes in a string (str) and a number (num)
// as arguments, truncates the string (str) after (num) no. of characters,
// adds an ellipsis(...) at the end and returns the truncated string.
//
// -------------------------------------------------------------------------
//
// Test Cases:
//
// The string 'Floccinaucinihilipilification' and the number 13 passed as
// arguments to the function returns a string.
//
// The string 'Floccinaucinihilipilification' and the number 13 passed as
// arguments to the function returns 'Floccinaucini...'
//
// The string 'Hello World' and the number 13 passed as
// arguments to the function returns 'Hello World'
//
// -------------------------------------------------------------------------

function truncateString(str, num){
console.log(str.length > num ? str.slice(0,num) + '...' : str)
}

truncateString('Floccinaucinihilipilification', 13)
// returns 'Floccinaucini...'

truncateString('Hello World', 13)
// returns 'Hello World'