001/*
002function showDay creates and returns a string,
003'Today is '
004suffixing the incoming value from the function call.
005A day name is expected, however other inputs are accepted
006parameter: day - expects a string
007return: a string beginning with "Today is "
008*/
009function showDay(day)
010{
011 return 'Today is '+ day;
012}
013
014/*
015function showDay2 validates the incoming value against
016a determined set of values (day names in English)
017and returns "Today is [day name]" else returns an error
018message if the input is not a day name
019Note: incomming day is accepted in any case
020*/
021function showDay2(day)
022{
023
024 const arrayOfDayNames = [
025 'Monday', 'Tuesday','Wednesday','Thursday',
026 'Friday','Saturday','Sunday'
027 ];
028
029 const errorMessage = 'The day is not recognised';
030
031 day = day.trim();
032
033 day = day.charAt(0).toUpperCase() + day.slice(1).toLowerCase();
034
035
036
037
038 return arrayOfDayNames.includes(day)?
039 'Today is '+ day : errorMessage;
040}
041
042/*
043function arraySumElements(anArray)
044expects an array of numbers and returns the sum of the values
045parameter: type-array - an array of numbers
046returns the sum of all elements
047*/
048function arraySumElements(anArray)
049{
050 return anArray.reduce((p,c)=>p+c,0);
051}
052
053/*
054function arraySumElements2(anArray)
055expects an array of numbers and returns the sum of the values
056parameter: type-array - an array of numbers
057returns the sum of all elements (if numerical)
058*/
059function arraySumElements2(anArray)
060{
061
062 let loopTotal = 0;
063
064
065 for(let i =0; i<anArray.length;i++)
066 {
067
068
069
070
071
072 if (anArray[i] === true) continue;
073
074
075
076
077 loopTotal += isNaN(anArray[i]) ? 0 : anArray[i];
078 }
079
080
081 return loopTotal;
082}
083/*
084function recieves an array of numbers,
085and calculates and returns the average, plus max and min value
086parameter: An array of numbers of any size
087returns: A 3 element array with average, min , max values
088NOTE: although in 1 line, this is not faster than using a loop
089and does not have any error checking of the array values
090*/
091function getArrayStats(arr)
092{
093 return [arr.reduce((p,c)=>p+c,0)/arr.length,Math.min(...arr),Math.max(...arr)];
094}
095
096/* try and work this out yourselves! */
097function getArrayStats2(arr)
098{
099 let total = 0;
let countOfValidValues =0;
let maxValue;
let minValue;
for(let i =0; i<arr.length;i++)
{
if(isNaN(arr[i]) || arr[i]===true)
{
continue;
}
countOfValidValues++;
if(maxValue==undefined && minValue==undefined)
{
maxValue = arr[i];
minValue = arr[i];
}
else
{
if(arr[i] > maxValue)
{
maxValue = arr[i];
}
if(arr[i] < minValue)
{
minValue = arr[i];
}
}
total += arr[i];
}
let average = 'No value can be computed';
if(countOfValidValues >0)
{
average = total/countOfValidValues;
}
return [average, minValue, maxValue];
}
const divider = '--------------------------------';
const realDay = 'Wednesday';
const messyDay = ' weDNESdaY ';
const fakeDay = 'Tobyday';
const numberArray = [2,4,1,10,5,2];
const mixedArray = [2,1,4,1,'alpha','beta', true];
const stringArray = ['alpha','beta', 'gamma'];
console.log('showDay: displays "Today is [day name]"') ;
console.log('');
console.log('"' + realDay + '"' + ' sent as argument, "Today is Wednesday" expected') ;
console.log(showDay(realDay));
console.log('');
console.log('"' + fakeDay + '"' + ' sent as argument, "Today is Tobyday" expected') ;
console.log(showDay(fakeDay));
console.log(divider);
console.log('showDay2: displays "Today is [day name]" or error')
console.log('');
console.log('"' + messyDay + '"' + ' sent as argument, "Today is Wednesday" expected') ;
console.log(showDay2(messyDay));
console.log('');
console.log('"' +fakeDay + '"' + ' sent as argument, error message expected') ;
console.log(showDay2(fakeDay));
console.log(divider);
let sumOfElements = arraySumElements(numberArray);
console.log('arraySumElements: sum of values in a number array. 24 expected') ;
console.log(sumOfElements);
console.log(divider);
sumOfElements = arraySumElements2(numberArray);
console.log('arraySumElements2: sum of values in a number array. 24 expected') ;
console.log(sumOfElements);
sumOfElements = arraySumElements2(mixedArray);
console.log('arraySumElements2: sum of numerical values in a mixed array. 8 expected')
console.log(sumOfElements);
sumOfElements = arraySumElements2(stringArray);
console.log('arraySumElements2: sum of numerical values in a string array. 0 expected')
console.log(sumOfElements);
console.log(divider);
let arrayStats = getArrayStats(numberArray);
console.log('getArrayStats: Using reduce() method and Built in Math Object');
console.log('');
console.log('the average, highest and lowest ' +
'are calculated on the array: \n[' +
numberArray.toString()+']');
console.log('');
console.log('The average number (expected is 4): ');
console.log(arrayStats[0]);
console.log('The highest number (expected is 10): ');
console.log(arrayStats[2]);
console.log('The lowest number (expected is 1): ');
console.log(arrayStats[1]);
console.log(divider);
arrayStats = getArrayStats2(numberArray);
console.log('');
console.log('getArrayStats2: Loop method (more lines of code, with error checking, but faster!)');
console.log('');
console.log('the average, highest and lowest ' +
'are calculated on the array: \n[' +
numberArray.toString()+']');
console.log('');
console.log('The average number (expected is 2): ');
console.log(arrayStats[0]);
console.log('The highest number (expected is 4): ');
console.log(arrayStats[2]);
console.log('The lowest number (expected is 1): ');
console.log(arrayStats[1]);
console.log('');
arrayStats = getArrayStats2(mixedArray);
console.log('the average, highest and lowest ' +
'are calculated on the array: \n[' +
mixedArray.toString()+']');
console.log('');
console.log('The average number (expected is 2): ');
console.log(arrayStats[0]);
console.log('The highest number (expected is 4): ');
console.log(arrayStats[2]);
console.log('The lowest number (expected is 1): ');
console.log(arrayStats[1]);
console.log('');
arrayStats = getArrayStats2(stringArray);
console.log('the average, highest and lowest ' +
'are calculated on the array: \n[' +
stringArray.toString()+']');
console.log('');
console.log('The average number (expected is error message): ');
console.log(arrayStats[0]);
console.log('The highest number (expected is error message): ');
console.log(arrayStats[0]);
console.log('The lowest number (expected is error message): ');
console.log(arrayStats[0]);
console.log('');