From 89a4a7f430d7c6a12cd5019529246866350dae54 Mon Sep 17 00:00:00 2001 From: Angelo Koddi Date: Tue, 3 Nov 2020 16:29:04 +0800 Subject: [PATCH] [javascript] add content in number --- JavaScript/number/getMaximum.js | 36 ++++++++++++++++++++++++ JavaScript/number/getMinimum.js | 31 +++++++++++++++++++++ JavaScript/number/onlyNegative.js | 36 ++++++++++++++++++++++++ JavaScript/number/onlyPositive.js | 36 ++++++++++++++++++++++++ JavaScript/number/reverseArray.js | 46 +++++++++++++++++++++++++++++++ 5 files changed, 185 insertions(+) create mode 100644 JavaScript/number/getMaximum.js create mode 100644 JavaScript/number/getMinimum.js create mode 100644 JavaScript/number/onlyNegative.js create mode 100644 JavaScript/number/onlyPositive.js create mode 100644 JavaScript/number/reverseArray.js diff --git a/JavaScript/number/getMaximum.js b/JavaScript/number/getMaximum.js new file mode 100644 index 00000000..a4ad6305 --- /dev/null +++ b/JavaScript/number/getMaximum.js @@ -0,0 +1,36 @@ +/* + * Copyright 2020 Koddi Evangelista + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +//this function will get the maximum number in an array +const getMax = (array) => { + // first, check if the input is an array, if not then return 'not an array' + if (!Array.isArray(array)) { + return "not an array"; + } + //copy an array using spread operator + let arrCopy = [...array]; + //iterate elements in array using .forEach() method + arrCopy.forEach((el, index) => { + //remove all the non-number in the array + if (typeof el !== "number") { + arrCopy.splice(index, 1); + } + }); + //sort the array + let sortedArray = arrCopy.sort((a, b) => a - b); + //and return the last index to get the highest number in an array. + return sortedArray[sortedArray.length - 1]; +}; +console.log(getMax([2, 3, "koddi", 5, 6, 7, 8, 9, 1])); diff --git a/JavaScript/number/getMinimum.js b/JavaScript/number/getMinimum.js new file mode 100644 index 00000000..a2a0e5da --- /dev/null +++ b/JavaScript/number/getMinimum.js @@ -0,0 +1,31 @@ +/* + * Copyright 2020 Koddi Evangelista + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +//this function will get the minimum number in an array +const getMax = (array) => { + // first, check if the input is an array, if not then return 'not an array' + if (!Array.isArray(array)) { + return "not an array"; + } + let arrCopy = [...array]; + arrCopy.forEach((el, index) => { + if (typeof el !== "number") { + arrCopy.splice(index, 1); + } + }); + let sortedArray = arrCopy.sort((a, b) => a - b); + return sortedArray[0]; +}; +console.log(getMax([2, 3, "koddi", 5, 6, 7, 8, 9, 1])); diff --git a/JavaScript/number/onlyNegative.js b/JavaScript/number/onlyNegative.js new file mode 100644 index 00000000..59c4ba8b --- /dev/null +++ b/JavaScript/number/onlyNegative.js @@ -0,0 +1,36 @@ +/* + * Copyright 2020 Koddi Evangelista + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +//this function will return only the negative numbers in an array +const onlyNegative = (array) => { + // first, check if the input is an array, if not then return 'not an array' + if (!Array.isArray(array)) { + return "not an array"; + } + //create an array where we will put all the negative number of input array + let onlyNegative = []; + //iterate the elements of array using .forEach() method + array.forEach((el, index) => { + // check if the type of element is number and is less than 0 + if (typeof el === "number" && el < 0) { + // if the element satisfies the condition push it the the array that we created + onlyNegative.push(array[index]); + } + }); + // return the onlyPositive array where we allocate all the negative numbers in input array + return onlyNegative; +}; + +console.log(onlyNegative(["koddi", "angelo", -5, -10, 1, 2, 3, 4])); diff --git a/JavaScript/number/onlyPositive.js b/JavaScript/number/onlyPositive.js new file mode 100644 index 00000000..09966c07 --- /dev/null +++ b/JavaScript/number/onlyPositive.js @@ -0,0 +1,36 @@ +/* + * Copyright 2020 Koddi Evangelista + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +//this function will return only the positive numbers in an array +const onlyPositive = (array) => { + // first, check if the input is an array, if not then return 'not an array' + if (!Array.isArray(array)) { + return "not an array"; + } + //create an array where we will put all the positive number of input array + let onlyPositive = []; + //iterate the elements of array using .forEach() method + array.forEach((el, index) => { + // check if the type of element is number and is greater than 0 + if (typeof el === "number" && el > 0) { + // if the element satisfies the condition push it the the array that we created + onlyPositive.push(array[index]); + } + }); + // return the onlyPositive array where we allocate all the positive numbers in input array + return onlyPositive; +}; + +console.log(onlyPositive(["koddi", "angelo", -5, -10, 1, 2, 3, 4])); diff --git a/JavaScript/number/reverseArray.js b/JavaScript/number/reverseArray.js new file mode 100644 index 00000000..9ec7730d --- /dev/null +++ b/JavaScript/number/reverseArray.js @@ -0,0 +1,46 @@ +/* + * Copyright 2020 Koddi Evangelista + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// this function will reverse an array using Array.reverse() +const reverseArray = (array) => { + // first, check if the input is an array, if not then return 'not an array' + if (!Array.isArray(array)) { + return "not an array"; + } + //use the .reverse() method a build-in method in javascript that reverses an array + return array.reverse(); +}; + +// this function will reverse an array using an algorithm +const reverseArrayAlgorithm = (array) => { + // first, check if the input is an array, if not then return 'not an array' + if (!Array.isArray(array)) { + return "not an array"; + } + //create a new array where we will place the reversed array + let reverseArr = []; + //get the length of input array, we will use it to iterating all the elements of array using for-loop + let length = array.length; + //create a for-loop that iterates the element of an array starting from the highest index to the lowest + for (let i = length - 1; i >= 0; i--) { + // push each element to the new array that we created + reverseArr.push(array[i]); + } + //return the reversed array + return reverseArr; +}; + +console.log(reverseArray([4, "koddi", 5, "angelo", 1, 2])); +console.log(reverseArrayAlgorithm([4, "koddi", 5, "angelo", 1, 2]));