Skip to content

Instantly share code, notes, and snippets.

View MbaziiraRonald's full-sized avatar
🧑‍💻
Working

Mbaziira Ronald MbaziiraRonald

🧑‍💻
Working
View GitHub Profile
@MbaziiraRonald
MbaziiraRonald / value-nearest-to-100.js
Last active August 17, 2021 13:41
A JavaScript program that returns a value nearest to 100, given two values.
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
@MbaziiraRonald
MbaziiraRonald / replace-first-character.js
Created August 17, 2021 13:27
A JavaScript program to replace the first character in a string with $. The string should be at least 2 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
}
@MbaziiraRonald
MbaziiraRonald / counting-string-vowels.js
Created August 17, 2021 13:03
A JavaScript program that returns the number of vowels in a string
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'));
@MbaziiraRonald
MbaziiraRonald / get-file-extension.js
Created August 17, 2021 08:36
A JavaScript program that returns the extension of a filename.
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'));
@MbaziiraRonald
MbaziiraRonald / gettingSubstring.js
Last active August 17, 2021 07:17
A JavaScript program to create a new string from a given string taking the first 3 characters and the last 3 characters of a string and adding them together. The string length must be 3 or more, if not the original string is returned.
//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) {
@MbaziiraRonald
MbaziiraRonald / gettingLatestArrayItem.js
Created August 16, 2021 14:42
Using slice method with a negative argument to get array items starting from the back or getting latest array items.
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]
@MbaziiraRonald
MbaziiraRonald / concatStringsExceptFirstCharacter.js
Last active August 16, 2021 14:39
A JavaScript program that concatenates two strings except their first character
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');
@MbaziiraRonald
MbaziiraRonald / checkingForEvenNumber.js
Last active August 17, 2021 07:18
A JavaScript program that checks if a given number is even or odd.
//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);
@MbaziiraRonald
MbaziiraRonald / joiningStringsExceptFirstCharacter.js
Last active August 16, 2021 14:50
A JavaScript program that concatenates two strings except their first character
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`));
@MbaziiraRonald
MbaziiraRonald / changeOriginalLetterCase.js
Last active August 11, 2021 14:09
A Javascript program that changes a string in lowercase to uppercase and a string in uppercase to lowercase.
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`))