From a698386db53842d6b61c0719d10d379f234f75ff Mon Sep 17 00:00:00 2001 From: "D.Morales" Date: Wed, 27 Jan 2021 14:42:47 -0500 Subject: [PATCH 1/2] DMorales ArraysWLoops --- problems/arraysWithLoops.js | 121 ++++++++++++++++++++++++++++-------- 1 file changed, 96 insertions(+), 25 deletions(-) diff --git a/problems/arraysWithLoops.js b/problems/arraysWithLoops.js index 4a8cfa3..f8cd178 100644 --- a/problems/arraysWithLoops.js +++ b/problems/arraysWithLoops.js @@ -1,4 +1,4 @@ -/** +/** DONE * Takes in an array and returns a new array with element * getting a ! added to the end. * Must use a for loop @@ -6,8 +6,17 @@ * @returns {Array} - ["I!", "am!", "a!", "happy!", "array!"] */ -function shoutForLoop() {} -/** +function shoutForLoop(arr) { + let newArr = []; + for (let i = 0; i < arr.length; i++) { + // Step toward making cond false + newArr.push(arr[i] + "!"); + } + return newArr; +} +// console.log(shoutForLoop(["I", "am", "a", "happy", "array"])) + +/**DONE * Takes in an array and returns a new array with element * getting a ! added to the end. * Must use a while loop @@ -15,9 +24,18 @@ function shoutForLoop() {} * @return {Array} - ["I!", "am!", "a!", "happy!", "array!"] */ -function shoutWhileLoop() {} - -/** +function shoutWhileLoop(arr) { + let newArr = []; + let i = 0; + while (i < arr.length) { + newArr.push(arr[i] + "!"); + i++; + } + return newArr; +} +// console.log(shoutWhileLoop(["I", "am", "a", "happy", "array"])) + +/**DONE * Takes in an array and returns a new array with element * getting a ! added to the end. * Must use a [for/of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) loop @@ -25,50 +43,103 @@ function shoutWhileLoop() {} * @returns {Array} - ["I!", "am!", "a!", "happy!", "array!"] */ -function shoutForOfLoop() {} +function shoutForOfLoop(arr) { + let newArr = []; + for (let el of arr) { + newArr.push(el + "!"); + } + return newArr; +} +console.log(shoutForOfLoop(["I", "am", "a", "happy", "array"])); -/** +/**DONE * Takes in an array and returns the sum of all values * @param {number[]} nums * @returns {number} sum */ -function sumArray() {} - -/** +// let arr = [1,2,3,4,5,6] +function sumArray(nums) { + let sum = 0; //index of zero and a place holder until incremented + for (i = 0; i < nums.length; i++) { + sum += nums[i]; //where the incrementation is changed + } + return sum; +} +// console.log(sumArray(arr)) + +/**DONE * Takes in an array and returns an array of all the odd valued elements * @param {number[]} nums * @returns {array} odds */ - -function oddArray() {} - -/** +// let arr = [1,2,3,4,5,6,7,8,9] +function oddArray(nums) { + let newArr = []; + for (let i = 0; i < nums.length; i++) { + if (nums[i] % 2 === 1) { + newArr.push(nums[i]); + } + } + return newArr; +} +// console.log(oddArray(arr)) + +/** DONE * Takes in an array and returns an array of all the even valued elements * @param {number[]} nums * @returns {array} evens */ -function evenArray() {} - +// let arr = [1,2,3,4,5,6,7,8,9] +function evenArray(nums) { + let newArr = []; + for (let i = 0; i < nums.length; i++) { + if (nums[i] % 2 === 0) { + newArr.push(nums[i]); + } + } + return newArr; +} +// console.log(evenArray(arr)) /** * Takes in array and returns the smallest number in the array * @param {number[]} nums * @returns {number} smallest value */ - -function findSmallest() {} - -/** +//arr = [14, 3, .5, 1]; +function findSmallest(nums) { + let smallest = nums[0]; + for (let i = 0; i < nums.length; i++) { + if (smallest > nums[i]) { + smallest = nums[i]; + } + } + return smallest; +} +//console.log(findSmallest(arr)); + +/** DONE got this info from the https://stackoverflow.com/questions/47316492/possible-to-use-math-min-to-get-second-smallest-number-from-array * Takes in array and returns the second smallest number in the array * You may NOT use sort * @param {number[]} nums * @returns {number} second smallest value */ -function findSecondSmallest() {} - -/** +function findSecondSmallest() { + let min = Infinity + secSmall = Infinity; +for (let i= 0; i< arr.length; i++) { + if (arr[i]< min) { + secSmall = min; + min = arr[i]; + } else if (arr[i]< secSmall ) { + secSmall = arr[i]; + } +} +// console.log('Second smallest number: ' + secSmall ); + +/** NOT SURE HOW TO DO * Takes in array and returns the second largest number in the array * @param {number[]} nums * @returns {number} second largest value @@ -76,7 +147,7 @@ function findSecondSmallest() {} function findSecondLargest() {} -/** +/**NOT SURE HOW TO DO * Takes in array and returns an array with all the values but with no duplicates. * Exp Input: [4,2,6,2,2,6,4,9,2,1] Output: [4, 2, 6, 9, 1] * @param {number[]} nums From a9b484f0df75e58f232bef7b542c925b111859ed Mon Sep 17 00:00:00 2001 From: "D.Morales" Date: Wed, 27 Jan 2021 15:20:20 -0500 Subject: [PATCH 2/2] DMorales ArraysWLoops --- problems/Notes.js | 1 + problems/loops.js | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 problems/Notes.js diff --git a/problems/Notes.js b/problems/Notes.js new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/problems/Notes.js @@ -0,0 +1 @@ + diff --git a/problems/loops.js b/problems/loops.js index f986f9f..d15c758 100644 --- a/problems/loops.js +++ b/problems/loops.js @@ -4,7 +4,14 @@ * @param {number} * @returns {number[]} */ -function oneTillDoneWhileLoop() {} +function oneTillDoneWhileLoop(number) { + let num = 1; + while (num <= number){ + num += 1 + } + return num +} +//console.log(oneTillDoneWhileLoop(30)) /** * Takes in a number and returns an array filled with all numbers from 1 to given number (inclusive). @@ -12,7 +19,12 @@ function oneTillDoneWhileLoop() {} * @param {number} * @returns {number[]} */ -function oneTillDoneForLoop() {} +function oneTillDoneForLoop() { + for (let num = 1; num < number; num += 1){ + + } + +} /** * Takes in a number and returns an array filled with all numbers from number to 0 (exclusive)