<input type="text" name="toTest" placeholder="enter the limit to test case">
<input type="button" value="Submit">
<div id="child">
</div>
<div id="result">
</div>
JavaScript
var testNo;
$("input[ type = 'button']").click(function(){
testNo = $("input[ name = 'toTest']").val();
$("#child").empty();
for(var i=0;i<testNo;i++){
$("#child").append(
"<br> <input type='text' id='size"+i+"' placeholder='enter the size of the array"+(i+1)+"'/> <input type='text' id='sumToCheck"+i+"' placeholder='enter the sum to be checked for array"+(i+1)+"'> <br>");
$("#child").append(
" <br> <input type='text' id='values"+i+"' placeholder='enter the elements of array"+(i+1)+"'/> <br>");
}
$("#child").append("<br> <button type='submit'> Result </button> <br>");
})
$("#child").on("click","button",function(){
$("#result").empty();
for(var i=0;i<testNo;i++){
var n = $("#size"+i).val();
var s = Number($("#sumToCheck"+i).val());
var a = $("#values"+i).val().split(",");
var sum = 0;
for(var j=0;j<n;j++){
sum = Number(a[j]);
var x;
for(x=j+1;x<n;x++){
sum += Number(a[x]);
if(sum == s){
break;
}
}if(sum == s){
$("#result").append("<p>"+(j+1)+" "+(x+1)+"</p>");
break;
}
}
if(sum != s){
$("#result").append("<p>-1 -1</p>");
}
}
})
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.