JavaScript 2022

Term 3 Wednesday

May - July 2022

This site will be updated live during the sessions

Week 2 Exercise 3

		
001// declare array
		
002let numbers =[ 10, 14, 32,42, 32, 199, 32, 40, 23];
		
003// use .length property to find out how long the array is
		
004// I am saving this as a variable just for easy access later
		
005let elementCountInNumbersArray = numbers.length;
		
006// create a counter to count how many values are in the correct range
		
007// this can be used for our new array index too.
		
008let counter = 0; //we start at 0 as the first element will be at index 0 
		
009//create a new (empty) array to store any numbers in the range
		
010let foundValuesArray = [];
		
011//create a for loop to go though each element in the array
		
012for(let i = 0; i < elementCountInNumbersArray; i++)
		
013{
		
014	if(numbers[i]>=30 && numbers[i] <=50)
		
015	{
		
016		//log the found value to the console
		
017		console.log("A new number was found:");
		
018		console.log(numbers[i]);
		
019		//add value at numbers[i] to the new array
		
020		foundValuesArray[counter] = numbers[i];
		
021		//add one to the counter
		
022		counter++
		
023	}
		
024}
		
025//after the loop finishes, log the values in the new array to the console
		
026console.log("The found numbers array is:");
		
027console.log(foundValuesArray.sort());