Skip to content
Merged
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
50 changes: 25 additions & 25 deletions archive/j/javascript/convex-hull.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@
*/
function convexHull(points) {
function orientation(p, q, r) {
const val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y)
if (val === 0) return 0
return val > 0 ? 1 : -1
const val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
if (val === 0) return 0;
return val > 0 ? 1 : -1;
}

const n = points.length
if (n < 3) return points
if (n < 3) return points;

let minPointIndex = 0
let minPointIndex = 0;
for (let i = 0; i < n; i++) {
if (
(points[i].y < points[minPointIndex].y) ||
(points[i].y == points[minPointIndex].y && points[i].x < points[minPointIndex].x)
) {
minPointIndex = i
minPointIndex = i;
}
}
const sortedPoints = [...points].sort((a, b) => {
const angleA = Math.atan2(a.y - points[minPointIndex].y, a.x - points[minPointIndex].x)
const angleB = Math.atan2(b.y - points[minPointIndex].y, b.x - points[minPointIndex].x)
return angleA - angleB
const angleA = Math.atan2(a.y - points[minPointIndex].y, a.x - points[minPointIndex].x);
const angleB = Math.atan2(b.y - points[minPointIndex].y, b.x - points[minPointIndex].x);
return angleA - angleB;
})

const convexHull = [sortedPoints[0], sortedPoints[1]]
const convexHull = [sortedPoints[0], sortedPoints[1]];

for (let i = 2; i < n; i++) {
while (
Expand All @@ -40,12 +40,12 @@ function convexHull(points) {
sortedPoints[i]
) !== -1
) {
convexHull.pop()
convexHull.pop();
}
convexHull.push(sortedPoints[i])
convexHull.push(sortedPoints[i]);
}

return convexHull
return convexHull;
}

/**
Expand All @@ -56,11 +56,11 @@ function convexHull(points) {
*/
function main(inputX, inputY) {
if (inputX == undefined || inputY == undefined){
inputX = " "
inputY = " "
inputX = " ";
inputY = " ";
}
const xArray = inputX.split(",").map(Number)
const yArray = inputY.split(",").map(Number)
const xArray = inputX.split(",").map(Number);
const yArray = inputY.split(",").map(Number);

if (
xArray.length < 3 ||
Expand All @@ -70,17 +70,17 @@ function main(inputX, inputY) {
) {
console.log(
'Usage: please provide at least 3 x and y coordinates as separate lists (e.g. "100, 440, 210")'
)
return
);
return;
}

const points = xArray.map((x, i) => ({ x, y: yArray[i] }))
const points = xArray.map((x, i) => ({ x, y: yArray[i] }));

const convexHullResult = convexHull(points)
convexHullResult.forEach((point) => console.log(`(${point.x}, ${point.y})`))
const convexHullResult = convexHull(points);
convexHullResult.forEach((point) => console.log(`(${point.x}, ${point.y})`));
}

// Run the executable function with command-line arguments
const inputX = process.argv[2]
const inputY = process.argv[3]
main(inputX, inputY)
const inputX = process.argv[2];
const inputY = process.argv[3];
main(inputX, inputY);
2 changes: 1 addition & 1 deletion archive/j/javascript/dijkstra.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ function main() {
console.log(dijkstra(src, des, n));
}

main()
main();
24 changes: 12 additions & 12 deletions archive/j/javascript/factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ function factorial(num) {
let product = 1;
if ( num > 1 ) {
for ( let i = 2; i <= num; i++ ) {
product *= i
product *= i;
}
}
return product
return product;
}

/**
Expand All @@ -24,36 +24,36 @@ function main(input) {

// No input
if ( !input ) {
console.log(usage)
return
console.log(usage);
return;
}

/**
* If we remove all the integer characters from the input and are left with
* an empty string, then we have a valid integer.
*/
const inputValidation = input.replace(/[0-9]/g,'')
const inputValidation = input.replace(/[0-9]/g,'');

if ( inputValidation === '' ) {
// Valid integer
const parsedInput = parseInt(input)
const parsedInput = parseInt(input);
if ( parsedInput < 0 ) {
console.log(usage)
console.log(usage);
}
else if ( parsedInput > 170 ) {
console.log(`Input of ${parsedInput} is too large to calculate a factorial for. Max input is 170.`)
console.log(`Input of ${parsedInput} is too large to calculate a factorial for. Max input is 170.`);
}
else {
console.log(factorial(parsedInput))
console.log(factorial(parsedInput));
}
}
else {
// Anything non integer
console.log(usage)
console.log(usage);
}

}

// Run the executable function
const input = process.argv[2]
main(input)
const input = process.argv[2];
main(input);
2 changes: 1 addition & 1 deletion archive/j/javascript/fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ num = process.argv[2];
if (num && !isNaN(num)) {
fibonacci(num);
} else {
console.log("Usage: please input the count of fibonacci numbers to output")
console.log("Usage: please input the count of fibonacci numbers to output");
}
2 changes: 1 addition & 1 deletion archive/j/javascript/hello-world.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log("Hello, World!")
console.log("Hello, World!");
2 changes: 1 addition & 1 deletion archive/j/javascript/insertion-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ sanitizeArray = (list) => {
const main = (input) => {
try {
arr = sanitizeArray(input);
arr.length <= 1 ? exit() : console.log(insertionSort(arr));
arr.length <= 1 ? exit() : console.log(insertionSort(arr).join(", "));
} catch(err) {
exit();
}
Expand Down
2 changes: 1 addition & 1 deletion archive/j/javascript/job-sequencing.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ const main = (string1, string2) => {
}
}

main(process.argv[2], process.argv[3])
main(process.argv[2], process.argv[3]);
14 changes: 7 additions & 7 deletions archive/j/javascript/linear-search.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
function LinSearch(arr = [], valToSearch) {
let check = false;
if (arr.length == 0) return check
if(valToSearch==='') return check
if (arr.length == 0) return check;
if(valToSearch==='') return check;
else {
for (i = 0; i < arr.length; i++) {
if (arr[i] == valToSearch){
check = true
return check
check = true;
return check;
}
}
return check
return check;
}
}

Expand All @@ -23,7 +23,7 @@ sanitizeArray = (list) => {

const exit = () => {
const usage = 'Usage: please provide a list of integers ("1, 4, 5, 11, 12") and the integer to find ("11")';
console.log(usage)
console.log(usage);
process.exit();
}

Expand All @@ -36,4 +36,4 @@ const main = (input, key) => {
}
}

main(process.argv[2], process.argv[3])
main(process.argv[2], process.argv[3]);
4 changes: 2 additions & 2 deletions archive/j/javascript/merge-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const main = (input) => {
});
arr = arr.filter(n => n);
arr=mergeSort(arr);
console.log(arr);
console.log(arr.join(", "));
}
else {
console.log(usage);
Expand All @@ -52,4 +52,4 @@ if (process.argv.length > 2) {
}
else {
console.log(usage);
}
}
4 changes: 2 additions & 2 deletions archive/j/javascript/prime-number.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const isPrime = (number) => {
if(number <= 1)
return false
return false;

for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
Expand All @@ -11,7 +11,7 @@ const isPrime = (number) => {
};

const input = process.argv[2];
let number = Number(input)
let number = Number(input);

if (input !== '' && Number.isInteger(number) && number >= 0) {
isPrime(input) ? console.log("prime") : console.log("composite");
Expand Down
36 changes: 17 additions & 19 deletions archive/j/javascript/quick-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@

const quickSort = (arr, start, end) => {
if(start < end) {
let pivot = partition(arr, start, end)
quickSort(arr, start, pivot - 1)
quickSort(arr, pivot + 1, end)
let pivot = partition(arr, start, end);
quickSort(arr, start, pivot - 1);
quickSort(arr, pivot + 1, end);
}
}

const partition = (arr, start, end) => {
let pivot = end
let i = start - 1
let j = start
let pivot = end;
let i = start - 1;
let j = start;
while (j < pivot) {
if (arr[j] > arr[pivot]) {
j++
j++;
} else {
i++
swap(arr, j, i)
j++
i++;
swap(arr, j, i);
j++;
}
}
swap(arr, i + 1, pivot)
return i + 1
swap(arr, i + 1, pivot);
return i + 1;
}

const swap = (arr, first, second) => {
let temp = arr[first]
arr[first] = arr[second]
arr[second] = temp
let temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}

const main = (input) => {
Expand Down Expand Up @@ -61,8 +61,8 @@ const main = (input) => {
arr = arr.filter(n => n);

// apply quicksort and output result
quickSort(arr, 0, arr.length - 1)
console.log(arr)
quickSort(arr, 0, arr.length - 1);
console.log(arr.join(", "));
}
else {
// invalid input
Expand All @@ -79,5 +79,3 @@ if (process.argv.length > 2) {
else {
console.log(usage);
}


2 changes: 1 addition & 1 deletion archive/j/javascript/quine.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(function(){console.log('('+arguments.callee.toString()+')()')})()
(function(){console.log('('+arguments.callee.toString()+')();')})();
2 changes: 1 addition & 1 deletion archive/j/javascript/selection-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ const main = (input) => {
}
}

main(process.argv[2])
main(process.argv[2]);
2 changes: 1 addition & 1 deletion archive/j/javascript/testinfo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ folder:

container:
image: "node"
tag: "11-alpine"
tag: "20-alpine"
cmd: "node {{ source.name }}{{ source.extension }}"