public static int factorial(int target) {
if (target > 0){
return target * factorial(target-1);
}
else if (target == 0){
return 1; //apparently "0! = 1"
}
else {
return -1; //not a possible value
}
}
System.out.println(factorial(-100));
popcorn hack #2 (sumArray)
public static int sumArray(int[] arr, int index) {
if(index < arr.length){
return arr[index]+sumArray(arr,index+1);
}
return 0;
}
int[] array = {0,1,2,3,4};
System.out.println(sumArray(array,0));
popcorn hack #3 selection sort
public static int[] selctionSort(int[] arr, int index, int max) {
if(index <= 1){ return selctionSort(arr,index+1,arr[0]);};
int[] temp = new int[arr.length];
int offset = 0;
if(arr[index] >= max){
temp[0] = arr[index];
temp[1] = arr[0];
arr[0] = arr[1];
arr[1] = temp[1];
arr[index] = arr[0];
offset = 1;
}
for(int i = offset; i<temp.length;i++){
temp[i]=arr[i];
}
System.out.println(Arrays.toString(temp));
if (index + 1 == arr.length){return temp;};
return selctionSort(temp,index + 1,temp[0]);
}
int[] array = {7,2,5,6};
System.out.println(Arrays.toString(selctionSort(array,0,array[0])));
[7, 2, 5, 6]
[7, 2, 5, 6]
[7, 2, 5, 6]