This file contains hidden or 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
let nearestTo100 = (a,b) => (100 - a) < (100 - b) ? a : b | |
// Testing the program | |
console.log(nearestTo100(40,29)); //40 | |
console.log(nearestTo100(10,29)); //29 | |
console.log(nearestTo100(101,29)); //101 | |
console.log(nearestTo100(50,29)); //50 |
This file contains hidden or 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
// Method 1 | |
let replaceFirstCharacter = (inputString) => inputString.length > 1 ? inputString.replace(inputString[(0)], '$') : inputString | |
//Method 2 | |
function replaceFirstCharacter(inputString) { | |
let myReplace = inputString.replace(inputString[(0)], '$'); | |
return myReplace | |
} |
This file contains hidden or 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 countStringVowels(inputString) { | |
let myVowels = inputString.match(/[aieou]/ig) | |
return myVowels.length; | |
} | |
//Testing the program | |
console.log(countStringVowels('money china')); | |
console.log(countStringVowels('Writing short functions is a good practice')); |
This file contains hidden or 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
let getFileExtension = (inputString) => inputString.slice(inputString.lastIndexOf('.')); | |
//Testing the program | |
console.log(getFileExtension('index.html')); | |
console.log(getFileExtension('panel.clouddra.com')); | |
console.log(getFileExtension('nira.co.ug')); |
This file contains hidden or 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
//Method 1 | |
let myNewString = (inputString) => inputString.length >= 3 ? `${inputString.substr(0,3)}${inputString.substr(-3)}` : inputString; | |
Testing the program | |
console.log(myNewString('one China and one Buganda')); | |
//Method 2 | |
function mySubString(inputString) { | |
if(inputString.length >= 3) { |
This file contains hidden or 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
const myArray = [1,2,3,4,50,56,23]; | |
const lastElement = myArray.slice(-1); | |
console.log(lastElement); // [23] | |
const twoSecondElement = myArray.slice(-2); | |
console.log(twoSecondElement); // [23,56] | |
const threeThirdElement = myArray.slice(-3); | |
console.log(threeThirdElement); // [23,56,50] |
This file contains hidden or 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
let concatTwoStrings = (stringOne,StringTwo) => ` ${stringOne.slice([1])}${StringTwo.slice([1])}`; | |
//Testing the program | |
console.log(concatTwoStrings('mone', 'china')); | |
console.log(concatTwoStrings('This is my life', 'Tell me about your life'); |
This file contains hidden or 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
//Method 1 | |
let checkForEvenNumbers = (number) => number % 2 === 0 ? console.log('This is an even number') : console.log('This is an odd number') | |
//Testing the program | |
checkForEvenNumbers(4); | |
checkForEvenNumbers(7); | |
checkForEvenNumbers(19); | |
checkForEvenNumbers(24); |
This file contains hidden or 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
let getHalfStringLength = (string1,string2) => string1.slice(1) + string2.slice(1); | |
// Testing the program | |
console.log(getHalfStringLength(`Am going back to Japan and china`, `love`)); | |
console.log(getHalfStringLength(`china`, `war`)); | |
console.log(getHalfStringLength(`Am going back `, `peace`)); |
This file contains hidden or 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 changeOriginalLetterCase(inputString) { | |
if(inputString === inputString.toLowerCase()) { | |
return inputString.toUpperCase(); | |
} else { | |
return inputString.toLowerCase(); | |
} | |
} | |
// Testing the program | |
console.log(changeOriginalLetterCase(`I LOVE GOING TO SCHOOL MUM BUT I LOVE PROGRAMMING MORE`)) |
NewerOlder