JavaScript 2022

Term 3 Wednesday

May - July 2022

This site will be updated live during the sessions

Week 3 Exercise 2

fca\FGHGFHDFHDHDHD
		
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	// create an array of allowed inputs
		
024	const arrayOfDayNames = [
		
025		'Monday', 'Tuesday','Wednesday','Thursday',
		
026		'Friday','Saturday','Sunday'
		
027	];
		
028	// create an message to feedback on error
		
029	const errorMessage = 'The day is not recognised';
		
030	// clean input - trim whiteSpace
		
031	day =  day.trim();
		
032	// split input string -make 1st char upper, rest lower
		
033	day =  day.charAt(0).toUpperCase() + day.slice(1).toLowerCase();
		
034
		
035	// Use a ternary statement to return either the
		
036	// string "Today is [day name]" if input is in array
		
037	// or an error message
		
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	// declare a variable to keep a running total
		
062	let loopTotal = 0;
		
063	
		
064	// create a loop, to iterate through the array elements by index
		
065	for(let i =0; i<anArray.length;i++)
		
066	{	
		
067		// CASE: element value is TRUE
		
068		// if the element value at arrayIndex[i]
		
069		// is the Boolean true,
		
070		// it will be recognised as the value 1, so 
		
071		// we need to skip it!
		
072		if (anArray[i] === true) continue;
		
073		
		
074		// if the array element value at arrayIndex[i]
		
075		// is not a number, add 0 to the running total,
		
076		//  if it is a numerical value add it
		
077		loopTotal += isNaN(anArray[i]) ? 0 : anArray[i];
		
078	}
		
079	
		
080	//return the total to the function call
		
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)// to avoid division by 0
		
	{
		
		average = total/countOfValidValues;
		
	}
		
	
		
	return [average, minValue, maxValue];
		
}
		
// END OF FUNCTIONS
		

		
//Declare some stuff
		

		
// string to log to divide exercises
		
const divider = '--------------------------------';
		
// some strings to use in the day exercises
		
const realDay  = 'Wednesday';
		
const messyDay = '  weDNESdaY  ';
		
const fakeDay  = 'Tobyday';
		
// some arrays to use in the following exercises
		
const numberArray = [2,4,1,10,5,2];
		
const mixedArray  =	[2,1,4,1,'alpha','beta', true];
		
const stringArray = ['alpha','beta', 'gamma'];
		

		

		
// Exercise 2 - 1a
		
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);
		

		
// Exercise 2 - 1b
		
// with error checking and santisation of input
		
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);
		

		

		

		

		
// Exercise 2 - 2a
		
// Uses the array method .reduce()
		
let sumOfElements = arraySumElements(numberArray);
		
console.log('arraySumElements: sum of values in a number array. 24 expected') ;
		
console.log(sumOfElements);
		
console.log(divider);
		

		
// Exercise 2 - 2b
		
// uses a loop and checks for numbers values in array elements
		
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);
		

		
// Exercise 2 - 3a
		
// uses array method .reduce and Math Object
		

		
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);
		

		
// Exercise 2 - 3a
		
// uses for loop and has (minimal) error checking
		
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('');