JSFiddle - React, Tailwind, and code Playground

by Murali Kaliappan

HTML

<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>");      	
      }
      
    
  }
})