Javascript Exercises
The following is a collection of functions from Learn Javascript
String Functions
Capitalize a string
/**
* @param {string} word
*/
function capitalize(word) {
const lowercaseWord = word.toLocaleLowerCase();
const firstLetter = word[0].toUpperCase();
const restOfWord = word.substring(1,word.length).toLowerCase();
const capitalizedWord = firstLetter + restOfWord;
return capitalizedWord;
}
// Sample usage - do not modify
console.log(capitalize("sam")); // "Sam"
console.log(capitalize("ALEX")); // "Alex"
console.log(capitalize("chARLie")); // "Charlie"
Numeric Functions
Accounting for an empty string or number from an input field.
This accounts for either an empty string or a null entry.
/**
* @param {string} age
*/
export function getNextAge(age) {
if (age.length === 0 || age === null) {
return 0;
} else {
const int = Number.parseInt(age, 10);
return int + 1;
}
}
Declare a function within a function (like a lambda in Ruby)
/**
* @param {number[]} numbers
*/
function sumOddNumbers(numbers) {
function isOddNumber(n) {
const number = Number.parseInt(n, 10);
return ( (number % 2 !== 0));
}
let total = 0;
numbers.forEach(function(number) {
if (isOddNumber(number) === true) {
total += number;
}
})
return total;
}
// Sample usage - do not modify
console.log(sumOddNumbers([15, 5, 10])); // 20
console.log(sumOddNumbers([2, 3, 4, 5, 6])); // 8
console.log(sumOddNumbers([-2, -3, 4, 5, 6])); // 2
Use a dictionary and a ternary to extract a substring and return a string
const dictionary = {
"°C": "Celsius",
"°F": "Fahrenheit"
}
const getUnit = text => {
const tempScan = /°[CF]+/.exec(text);
const tempString = tempScan === null ? 'N/A' : dictionary[tempScan[0]];
return tempString;
}
// Sample usage - do not modify
console.log(getUnit("It is 15°C today")); // Celsius
console.log(getUnit("It was 90°F yesterday")); // Fahrenheit
console.log(getUnit("Why is it 20°C")); // Celsius
console.log(getUnit("Are you sure that it'll be 55°F?")); // Fahrenheit
console.log(getUnit("It is expected to be cold.")); // N/A
Published on 22 Jan 2023
all tags
100daysofcode activerecord android annoyances api apt arch array artix atom az3w backend bash blog browser bug callback career ci-cd cli cloud code coding config configuration cp crud cryptography css csv database db design devops django docker email erp feelsgood filter fugitive gif gist git gnome gnome pomodoro grep hebrew http ide isbn-fetcher iso javascript job search js kanban kindle koans learning linkedin linux logger manjaro map markdown microservices mobi mtp neovim nodejs nvchad packages panda pastbin patch portfolio post postgres pytest python rails reduce refactoring reflections rest routes rspec ruby salesforce script scripting security sed shell sql string_replacement study tdd terminal testing tmux ttd version_control vim vim sort walkthrough webdev workflow zsh