All puzzles
Per page
G
String · Property · Early return

Is there a digit?

Return true if code contains at least one digit, and false if it does not.

hasDigit("abc0") returns true. hasDigit("abc") returns false.

10 strips: G1 to G10

for (int i = 0; i < code.length(); i++) {
G1
return true;
G2
if ("0123456789".indexOf(ch) > 0) {
G3
return false;
G4
if ("0123456789".indexOf(ch) != -1) {
G5
}
G6
public static boolean hasDigit(String code) {
G7
}
G8
}
G9
String ch = code.substring(i, i + 1);
G10

Answer key: G. Is there a digit?

Order: G7, G1, G10, G5, G2, G9, G6, G4, G8. Extra: G3

public static boolean hasDigit(String code) {
	for (int i = 0; i < code.length(); i++) {
		String ch = code.substring(i, i + 1);
		if ("0123456789".indexOf(ch) != -1) {
			return true;
		}
	}
	return false;
}

Extra strip: if ("0123456789".indexOf(ch) > 0) {. indexOf returns 0 for "0", so > 0 treats the digit 0 as not found. Not found is -1.