I'm at the stage in Java learning where, I've gone through basic tutorials, etc, but I need to be stepping up the complexity of my coding. Although the codewars 1st lesson seems borderline beyond me :(
I'm not particularly looking for the answer (cheating), but perhaps someone can critique the (very little) that I've done, and point me in the right direction. Here's the question:
Write a function that can return the smallest value of an array or the index of that value. The function's 2nd parameter will tell whether it should return the value or the index.
Assume the first parameter will always be an array filled with at least 1 number and no duplicates. Assume the second parameter will be a string holding one of two values: 'value' and 'index'.
Here's my initial code...
public class Arrays {
public static int findSmallest( final int[] numbers, final String toReturn ) {
//TODO: Add solution here
int smallest = numbers[0];
for (int i = 0; i < numbers.length; i++)
{
if (numbers[i] <= smallest)
{
smallest = numbers[i];
}
}
return smallest;
System.out.println(smallest);
}
}
Error:
/Arrays.java:14: error: unreachable statement System.out.println(smallest); ^ /Arrays.java:16: error: missing return statement } ^ 2 errors
I've been bashing my head about the overall logic I've been using to make steps forward with this challenge. Can anyone point out the error of my ways? Thanks for reading a long-winded description of an otherwise, I'm sure, simple problem....
*edit - please note I've not even began to deal with the first part of the challenge, which is making the distinction between returning the value or the place in the array.