Let us learn to rewind in JS…
Loops
For-of loop
This loop is handy if we’re iterating items in a list :
const spotifyTopTen = new Array(
"Paint My Soul", "The Nights",
"Send Them Off!", "Everlasting",
"Good Grief", "Hangin'",
"Soy Yo", "Walk",
"Hey Brother", "My Blood"
);
for (let song of spotifyTopTen)
{
console.log(song);
}
map()
let’s you do stuff with the individual items of an array :
function toUpper(array)
{
let string = array.toString();
return string.toUpperCase();
}
const weightOfLiving = [
"Or under the weight of living?",
"You're under the weight of living",
"Under the weight of living",
"You are under the weight of living",
"The weight of living",
"The weight of living"
];
const mapping = weightOfLiving.map(toUpper);
console.log(mapping);
/*
[
'OR UNDER THE WEIGHT OF LIVING?',
"YOU'RE UNDER THE WEIGHT OF LIVING",
'UNDER THE WEIGHT OF LIVING',
'YOU ARE UNDER THE WEIGHT OF LIVING',
'THE WEIGHT OF LIVING',
'THE WEIGHT OF LIVING'
]
*/
Exercises
Write a function that repeats the string a given number of times :
function repeatString(string, howManyTimes)
{
let finalString = "";
for (let i=0; i<howManyTimes; i++)
{
finalString += string;
}
return finalString;
}
console.log(repeatString("Weight of Living ", 4));
Alternate way to solve using String.repeat()
function repeatString(string, howManyTimes)
{
return string.repeat(howManyTimes);
}
console.log(repeatString("Weight of Living ", 4));
Write a function that returns its input reversed :
function reverseString(string)
{
const array = string.split("");
let finalString = "";
let stringLength = array.length-1;
for (let i=stringLength; i>=0; i--)
{
finalString += array[i];
}
return finalString;
}
console.log(reverseString("Does it almost feel like you've been here before?"));
Implement a function that takes 2 positive integers and returns the sum of every integer between (and including) them:
function sumAll(intOne, intTwo)
{
let sum = 0;
for (let i=intOne; i<=intTwo; i++)
{
sum += i;
}
return sum;
}
console.log(sumAll(32, 99));
let start = 23;
let end = 246;
const funct = (start, end) =>
{
let sum = 0;
for (let i=start; i<=end; i++)
{
sum += i;
}
return sum;
}
console.log(funct(start, end));
Create a function that determines whether or not a given year is a leap year.
Leap years are years divisible by four (like 1984 and 2004). However, years divisible by 100 are not leap years (such as 1800 and 1900) unless they are divisible by 400 (like 1600 and 2000, which were in fact leap years).
function leapYears(year)
{
if (year%4==0)
{
if (year%100==0 && year%400==0)
{
return true;
}
return false;
}
return false;
}
const array = [1800, 2000, 1985, 1600, 1030];
for (let i in array)
{
console.log(leapYears(array[i]));
}
function leapYears(year)
{
(year%4==0) ? ((year%100==0 && year%400==0) ?
console.log(true) : console.log(false)) :
console.log(false);
}
const array = [1800, 2000, 1985, 1600, 1030];
for (let i in array)
{
console.log(leapYears(array[i]));
}
Write two functions that convert temperatures from Fahrenheit to Celsius, and vice versa:
function convertToCelsius(tempInFahrenheit)
{
return ((tempInFahrenheit - 32) * 5/9).toFixed(1);
}
function convertToFahrenheit(tempInCelsius)
{
return ((tempInCelsius * 9/5) + 32).toFixed(1);
}
console.log(convertToCelsius(90));
console.log(convertToFahrenheit(45));
function tempConversion(unit, temperature)
{
if (unit.toLowerCase()=="celsius" || unit=="c")
{
return ((temperature - 32) * 5/9).toFixed(1);
}
else if (unit.toLowerCase()=="fahrenheit" || unit=="f")
{
return ((temperature * 9/5) + 32).toFixed(1);
}
}
console.log(tempConversion("c", 112));
Create a calculator that does the following: add, subtract, get the sum, multiply, get the power, and find the factorial:
function add (num, ...args)
{
return num + args;
}
function subtract (num, ...args)
{
return num - args;
}
function multiply (num, ...args)
{
return num * args;
}
function division (num, ...args)
{
if (args===0 || isNaN(args))
{
return null;
}
else
{
return num / args;
}
}
function exponent (num, ...args)
{
return num ** args;
}
function fact (num)
{
let factNum = 1;
for (let i=1; i<=num; i++)
{
factNum *= i;
}
return factNum;
}