SUM OF RANGE
<html>
<head>
<title>PART3</title>
<h1>PART3param</h1>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h2>SumRange</h2>
<input type ="number" id="StartVal" value=0 title="Start value"/>
<input type="number" id="EndVal" value=0 title="End value"/>
<input type="number" id="Step" value=0 title="input step interval"/>
<button id="Calc">Calculate</button>
<br><br>
<h4>ListNumbers:</h4><br>
<ol id="ordList"></ol><br>
<h4 id="SumNum">Sum of numbers:</h4><br>
<script>
//Create array of numbers
function MakeArray(a,b,c) {
var numbers = [];
var numList = $("#ordList");
var sumall = 0;
numbers.length = 0;
//check negative --> switch vars
if ( Number(c) < 0 ) {
//make array
for (i=Number(b); i>=Number(a) ; i=i+Number(c)) {
numbers.push(i);
sumall += i;
$("<li/>").text(i).appendTo(numList);
}
}
else {
//make array
for (i=Number(a); i<=Number(b) ; i=i+Number(c)) {
numbers.push(i);
sumall += i;
$("<li/>").text(i).appendTo(numList);
}
}
$("#SumNum").text("Sum of all numbers in array= " +sumall);
console.log(numbers)
};
//when button clicked
$( "#Calc" ).click(function() {
$("#ordList").html("");
MakeArray( $('#StartVal').val(), $('#EndVal').val(), $('#Step').val() );
});
</script>
</body>
</html>
Reversing array
<html>
<head>
<title>PART3</title>
<h1>P3: ReverseArray</h1>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h2>ReverseArray</h2>
<input type ="number" id="StartVal" value=0 title="Start value"/>
<input type="number" id="EndVal" value=0 title="End value"/>
<input type="number" id="Step" value=0 title="input step interval"/>
<button id="Calc">Process</button>
<br><br>
<h4>ListNumbers:</h4><br>
<ol id="ordList"></ol><br>
<h4>ListNumbers ReverseArray:</h4><br>
<ol id="ordListRev"></ol><br>
<h4>ListNumbers ReverseArrayInPlace:</h4><br>
<ol id="ordListRevIp"></ol><br>
<script>
var numbers = [];
var RevNumbers = [];
function showarray(ar,name){
//show an array that comes in: par1 is the array itsel, par2 is the name of the array
$.each(ar, function( i, value ) {
$("#"+name).append("<li>"+ value + "</li>");
});
};
//Create array of numbers
function MakeArray(a,b,c) {
numbers.length = 0; // make sure array is empty
//make numbers array based on parameters
for (i=Number(a); i<=Number(b) ; i=i+Number(c)) {
numbers.push(i);
}
showarray(numbers,"ordList");
};
//reverse an array that comes in and store it in another array
function ReverseArray(a) {
RevNumbers.length = 0; // make sure array is empty
for (i=a.length; i > 0 ; i--) {
RevNumbers.push([i]);
}
showarray(RevNumbers,"ordListRev");
};
//reverse an array that comes in and store it in the same array
function ReverseArrayInPlace(a) {
//make temp array
var temparray = [];
temparray.length = 0; // make sure array is empty
var b = 0;
for (i=a.length-1; i >= 0 ; i--) {
temparray.push(a[i]);
b++;
}
a.length = 0; //empty original array
for (i=0; i < temparray.length ; i++) {
a.push(temparray[i]);
}
// temparray.length = 0;
showarray(a,"ordListRevIp");
};
//when button clicked
$( "#Calc" ).click(function() {
$("#ordList").html("");
$("#ordListRev").html("");
$("#ordListRevIp").html("");
MakeArray( $('#StartVal').val(), $('#EndVal').val(), $('#Step').val() );
ReverseArray(numbers);
ReverseArrayInPlace(numbers);
});
</script>
</body>
</html>