Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[javascript] add content in number #224

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions JavaScript/number/getMaximum.js
Original file line number Diff line number Diff line change
@@ -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]));
31 changes: 31 additions & 0 deletions JavaScript/number/getMinimum.js
Original file line number Diff line number Diff line change
@@ -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]));
36 changes: 36 additions & 0 deletions JavaScript/number/onlyNegative.js
Original file line number Diff line number Diff line change
@@ -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]));
36 changes: 36 additions & 0 deletions JavaScript/number/onlyPositive.js
Original file line number Diff line number Diff line change
@@ -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]));
46 changes: 46 additions & 0 deletions JavaScript/number/reverseArray.js
Original file line number Diff line number Diff line change
@@ -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]));