{"record":{"id":"25df9f2a7e3f14c2","repo":"karatelabs/karate","slug":"repeat-needs-two-arguments-count-and-function","errorCode":null,"errorMessage":"repeat() needs two arguments: count and function","messagePattern":"repeat\\(\\) needs two arguments: count and function","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":452,"sourceCode":"        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"prettyXml() needs one argument\");\n            }\n            Object obj = args[0];\n            if (obj instanceof Node) {\n                return Xml.toString((Node) obj, true);\n            } else if (obj instanceof String) {\n                return Xml.toString(Xml.toXmlDoc((String) obj), true);\n            } else {\n                throw new RuntimeException(\"prettyXml() argument must be XML node or string\");\n            }\n        };\n    }\n\n    static JavaInvokable repeat() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"repeat() needs two arguments: count and function\");\n            }\n            int count = ((Number) args[0]).intValue();\n            JavaCallable fn = (JavaCallable) args[1];\n            List<Object> result = new ArrayList<>();\n            for (int i = 0; i < count; i++) {\n                result.add(fn.call(null, new Object[]{i}));\n            }\n            return result;\n        };\n    }\n\n    static JavaInvokable sizeOf() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"sizeOf() needs one argument\");\n            }\n            Object obj = args[0];\n            if (obj instanceof List) {","sourceCodeStart":434,"sourceCodeEnd":470,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L434-L470","documentation":"repeat() is a Karate JS utility that calls a function N times (the function receives the index) and collects results in a List. It throws this error when called with fewer than two arguments, because both the count and the function are mandatory.","triggerScenarios":"karate.repeat() with zero or one argument, e.g. repeat(5) without the function, or repeat() entirely; a null count variable reducing the bound arguments.","commonSituations":"Forgetting the callback when translating a for-loop; passing the arguments in the wrong order; the function variable is undefined so the arity check fails.","solutions":["Supply both arguments: repeat(count, fn) where fn(i) returns the value for each iteration.","Ensure the first argument is a number and the second an actual function.","Verify the count is defined and non-null before the call."],"exampleFix":"// before\ndef list = repeat(3)\n// after\ndef list = repeat(3, function(i){ return 'item' + i })","handlingStrategy":"validation","validationCode":"def list = (count && fn) ? repeat(count, fn) : []","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Signature is repeat(count, function(i){...}) — two arguments always, in that order.","Define the callback function before calling repeat().","Verify count is a number; define it in a prior step if computed."],"tags":["karate","javascript","argument-count","lists"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}