Skip to content

Instantly share code, notes, and snippets.

@DevGW
Last active April 5, 2023 14:36
Show Gist options
  • Save DevGW/9e15501c3f1663de15a62298179444fd to your computer and use it in GitHub Desktop.
Save DevGW/9e15501c3f1663de15a62298179444fd to your computer and use it in GitHub Desktop.
Javascript :: defining constants w/ default params #js
const exponentiate = (base, power) => {
let exponentiatedValue = 1;
for (i = 1; i <= power; ++i) {
exponentiatedValue *= base;
}
return exponentiatedValue;
}
// can do the same thing with:
const exponentiate = (base, power) => base ** power;
// other example
// Default Parameters
// A way to stipulate what the value of an argument is should one not be passed into function
const mySlice = (source, startIdx = 0, endIdx = source.length) => {
let slicedString = '';
for (let i = startIdx; i < endIdx; ++i) {
const curChar = source[i];
slicedString += curChar;
}
return slicedString
}
const myIndexOf = (source, searchValue, startIdx = 0) => {
for (let i = startIdx; i <= source.length - searchValue.length; i++) {
let substring = source.slice(i, i+searchValue.length);
if (substring === searchValue) {
return i;
}
} return -1;
}
// Most vowels word return from a sentence. Example:
const mostVowels = (aStr) => {
const wordsArray = aStr
.split('.')
.join('')
.split(',')
.join('')
.split(' ');
const VOWELS = 'aeiouAEIOU';
let maxVowelCount = 0;
let maxWord = '';
for (let i = 0; i < wordsArray.length; ++i) {
const currentWord = wordsArray[i];
let currentVowelCount = 0;
// This loops job is to count vowels per word, when its done looping we have finished for this word.
for (var j=0; j < currentWord.length; ++j) {
const currentLetter = currentWord[j];
// We found a vowel!
if (VOWELS.indexOf(currentLetter) !== -1) {
++currentVowelCount;
}
}
// Were done counting.
if (currentVowelCount > maxVowelCount) {
maxVowelCount = currentVowelCount;
maxWord = currentWord;
}
}
return maxWord;
// This outer loop says we went through each word, updating the maxWord if it had the most vowels.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment