This took me quiet a while TBH. Kept running into what turned out to be the tiniest problems mostly, only after hours of googling lol. ALL array tasks (show, sum, step, reverse, and new reverse) included in this one code.
<body>
<label>Enter a Min number:<br /></label>
<input type="number" id="minNum" name="minNum" /><br />
<label><br />Enter a Max number:<br /></label>
<input type="number" id="maxNum" name="maxNum" /><br />
<label><br />Enter a step (optional):<br /></label>
<input type="number" id="stepNum" name="stepNum" /><br />
<br />
<button id="showArrayBtn">Show array</button>
<button id="sumArrayBtn">Sum of array</button>
<button id="stepArrayBtn">Array in steps</button>
<br /><br />
<button id="rvsArrayBtn">Reverse array</button>
<button id="newRvsArrayBtn">NEW reverse array</button>
<br /><br />
<script>
// global var
var arr = [];
// button handlers
$("#showArrayBtn").click(function() {
range("show");
});
$("#sumArrayBtn").click(function() {
range("sum");
});
$("#stepArrayBtn").click(function() {
range("step");
});
$("#rvsArrayBtn").click(function() {
if (!arr.length) { // is there an array
console.log("Please create an array first.");
} else {
arr = reverseArray(arr);
console.log(arr);
}
});
$("#newRvsArrayBtn").click(function() {
if (!arr.length) { // is there an array
console.log("Please create an array first.");
} else {
reverseArrayInPlace(arr);
}
});
// functions
function range(chosenBtn) {
var stepVal = parseInt($("#stepNum").val(), 10);
var minVal = parseInt($("#minNum").val(), 10);
var maxVal = parseInt($("#maxNum").val(), 10);
if (chosenBtn == "show") {
arr = [];
for (x = minVal; x <= maxVal; x++) {
arr.push(x);
}
console.log(arr);
} else if (chosenBtn == "sum") {
arr = [];
var arrSum = 0;
for (x = minVal; x <= maxVal; x++) {
arr.push(x);
arrSum += x;
}
console.log(arrSum);
} else if (chosenBtn == "step") {
// is a positive int
arr = [];
for (x = minVal; x <= maxVal; x += stepVal) {
arr.push(x);
}
console.log(arr);
} else if (stepVal < 0) {
// is a negative int
arr = [];
for (x = minVal; x >= maxVal; x += stepVal) {
arr.push(x);
}
console.log(arr);
} else {
range("show");
}
}
function reverseArray(nRa) {
var tempRvsArr = [];
for (c = 0; c <= nRa.length; c++) {
if (c > 0) {
tempRvsArr.push(nRa[nRa.length - c]);
}
}
return tempRvsArr;
}
function reverseArrayInPlace(rA) {
var tempRvsArr = [];
for (c = 0; c <= rA.length; c++) {
if (c > 0) {
tempRvsArr.push(rA[rA.length - c]);
}
}
console.log(tempRvsArr);
}
</script>
</body>