Reputation: 8589
I am trying to update the DOM depending on the values given by the user.
I have this function:
function iterateOverArray(array){
var oldArray = array;
var newArray = "";
for (var i = 0; i < oldArray.length; i++) {
if (oldArray.includes(-1)) {
newArray += " - " + oldArray[i];
} else {
newArray += " + " + oldArray[i];
}
}
return newArray;
}
The program should work like this:
If the array given is: [1, 5, 10]
then you should see in the DOM this: 1 + 5 + 10
but if the array given is: [10, 2, -1, 10]
then what you should see in the DOM is: 10 + 2 - 10
the negative number -1
should always represent -
in the DOM.
What I am getting now is this:
And I don't want that, I just don't want to see that -1
, I need only the -
(subtract) sign.
The other issue I am having is that once you put a negative number, the new values/numbers that will see in the DOM comes with the negative sign even if the value given it's positive:
Here is the code if you want to take a look
Any suggestions ?
Upvotes: 0
Views: 64
Reputation: 3032
Try changing the if statement check if the previous item is -1
. If it was, then append -
. Otherwise, if this is the first item or the previous item was -1
, append the current item. Otherwise, append +
plus the current item:
if (oldArray[i] == -1) {
newArray += " - ";
} else if (i == 0 || oldArray[i - 1] == -1) {
newArray += oldArray[i];
} else {
newArray += " + " + oldArray[i];
}
var arrayOfNumbers = [];
var column = 1;
function logingValues() {
var values = $('input[name^=valuesToSum]').map(function(idx, elem) {
newVal = $(elem).val();
if (isNaN(newVal)) {
newVal = 0;
}
return parseInt(newVal);
}).get();
//console.log("Values aquired from inputs " + values);
arrayOfNumbers = values;
//console.log("Values inserted on Array " + arrayOfNumbers);
}
function removeElement(id) {
$('#' + id).remove();
//console.log("element #" + id + " was eliminated");
}
function iterateOverArray(array) {
var oldArray = array;
var newArray = "";
for (var i = 0; i < oldArray.length; i++) {
if (oldArray[i] == -1) {
newArray += " - ";
} else if (i == 0 || oldArray[i - 1] == -1) {
newArray += oldArray[i];
} else {
newArray += " + " + oldArray[i];
}
//console.log('__newArray', newArray);
//console.log('__oldArray', oldArray);
}
return newArray;
}
function addElement(whereToPrint) {
logingValues();
var newArray = iterateOverArray(arrayOfNumbers);
var printId = "print" + whereToPrint;
//console.log(printId);
//console.log("we can see the array like this " + arrayOfNumbers);
$('#' + printId).html(newArray);
//console.log('Element was created');
column += 1;
var newInput = '<div class="row" id="' + column + '">';
newInput = newInput + '<div class="col-lg-3 col-md-4 col-sm-2">';
newInput = newInput + '<input name="valuesToSum" type="text" placeholder="Place a Number" required />';
newInput = newInput + '</div>';
newInput = newInput + '<div class="col-lg-3 col-md-4 col-sm-2">';
newInput = newInput + '<span id="print' + column + '"></span>';
newInput = newInput + '</div>';
newInput = newInput + '<div class="col-lg-4 col-md-2 col-sm-4">';
newInput = newInput + '<button class="btn-danger rem" onclick="removeElement(' + column + ')">-</button>';
newInput = newInput + '<button class="btn-default sum new" onclick="addElement(' + column + ')">+</button>';
newInput = newInput + '</div>';
newInput = newInput + '<div class="col-lg-2 col-md-2 col-sm-4 animation">';
newInput = newInput + '<div class="spinner">';
newInput = newInput + '</div>';
newInput = newInput + '</div>';
newInput = newInput + '</div>';
$('.elemPlaceHold').append(newInput);
}
function loadingBar() {
var summingBar = '<div class="rect1"></div><div class="rect2"></div><div class="rect3"></div><div class="rect4"></div><div class="rect5"></div>';
$('.spinner').append(summingBar);
}
function caculationEffect(countTotal) {
$('#result').html("");
loadingBar();
setTimeout(function() {
$('.spinner').html("");
$('#result').html(countTotal);
}, 2000);
}
function calculate() {
//console.log("is calculating....");
logingValues();
var total = 0;
var shouldSubtract = false;
for (var i = 0; i < arrayOfNumbers.length; i++) {
if (arrayOfNumbers[i] === "") continue;
var currentNumber = parseInt(arrayOfNumbers[i], 10);
if (isNaN(currentNumber)) {
currentNumber = 0;
}
if (currentNumber === -1) {
shouldSubtract = true;
} else {
if (shouldSubtract) {
total -= currentNumber;
shouldSubtract = false;
} else {
total += currentNumber;
}
}
}
caculationEffect(total);
//console.log('countTotal', total);
}
.spinner {
width: 50px;
height: 40px;
text-align: center;
font-size: 10px;
}
.spinner > div {
background-color: #333;
height: 100%;
width: 6px;
display: inline-block;
-webkit-animation: sk-stretchdelay 1.2s infinite ease-in-out;
animation: sk-stretchdelay 1.2s infinite ease-in-out;
}
.spinner .rect2 {
-webkit-animation-delay: -1.1s;
animation-delay: -1.1s;
}
.spinner .rect3 {
-webkit-animation-delay: -1.0s;
animation-delay: -1.0s;
}
.spinner .rect4 {
-webkit-animation-delay: -0.9s;
animation-delay: -0.9s;
}
.spinner .rect5 {
-webkit-animation-delay: -0.8s;
animation-delay: -0.8s;
}
@-webkit-keyframes sk-stretchdelay {
0%, 40%, 100% {
-webkit-transform: scaleY(0.4)
}
20% {
-webkit-transform: scaleY(1.0)
}
}
@keyframes sk-stretchdelay {
0%, 40%, 100% {
transform: scaleY(0.4);
-webkit-transform: scaleY(0.4);
}
20% {
transform: scaleY(1.0);
-webkit-transform: scaleY(1.0);
}
}
.rem,
.sum {
border-radius: 100%;
}
.new {
margin-left: 5px;
}
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="css/main.css">
<!-- JQuery -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<h1>Array Calculator</h1>
<div class="jumbotron">
<p>Please create your array by filling spaces with Numbers, its calculation will be based on provided logic.</p>
<form class="container elemPlaceHold">
<div class="row" id="1">
<div class="col-lg-3 col-md-4 col-sm-2">
<input name="valuesToSum" type="text" placeholder="Place a Number" required />
</div>
<div class="col-lg-3 col-md-4 col-sm-2">
<span id="print1"></span>
</div>
<div class="col-lg-4 col-md-2 col-sm-4">
<button class="btn-default sum" onclick="addElement(1)">+</button>
</div>
<div class="col-lg-2 col-md-2 col-sm-4 animation">
<div class="spinner"></div>
</div>
</div>
</form>
<br>
<div>
<p>The total calculation of the array is <span id="result"></span>
</p>
</div>
<br>
<button class="btn btn-danger" onclick="calculate()">Animate</button>
</div>
</div>
<footer>
<script type="text/javascript" src="js/app.js"></script>
</footer>
</body>
</html>
Upvotes: 1
Reputation: 21575
The condition oldArray.includes(-1)
will always be true when -1
is anywhere in the array. That's why they all become -
, looks like you just want to check for the current index.
To remove the -1
from showing up you can change newArray += " - " + oldArray[i]
to newArray += " - "
. With those changes the code would look like so:
function iterateOverArray(array){
var oldArray = array;
var newArray = "";
for (var i = 0; i < oldArray.length; i++) {
if (oldArray[i] == -1) {
newArray += " - ";
} else {
newArray += " + " + oldArray[i];
}
}
newArray = newArray.replace(/\- \+/, "-"); // See information below
return newArray;
}
Which will be close to what you want. For example the input [10, 2, -1, 10]
would return + 10 + 2 - + 10
. Finally to get rid of the "- +"
you can use .replace()
. So add newArray = newArray.replace(/\- \+/, "-");
right before returning. That will replace all instances of the sub-string "- +"
with simply "-"
.
function iterateOverArray(array){
var oldArray = array;
var newArray = "";
for (var i = 0; i < oldArray.length; i++) {
if (oldArray[i] == -1) {
newArray += " - ";
} else {
newArray += " + " + oldArray[i];
}
}
newArray = newArray.replace(/\- \+/, "-");
return newArray;
}
console.log(iterateOverArray([10, 2, -1, 10]))
Upvotes: 1