Don't declare a new str variable inside the loop with var str. Reuse the one you declare outside the loop. Also do +=
var divLength = $('div').length;
var str = '';
for(var i = 0; i < divLength; i++) {
str += "Div #" + i + ", ";
console.log(str);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>
<div></div>
<div></div>
<div></div>
Answer from Red Mercury on Stack OverflowDon't declare a new str variable inside the loop with var str. Reuse the one you declare outside the loop. Also do +=
var divLength = $('div').length;
var str = '';
for(var i = 0; i < divLength; i++) {
str += "Div #" + i + ", ";
console.log(str);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>
<div></div>
<div></div>
<div></div>
Besides the selected answer, you could do this with a forEach if you have a list of stuff to put inside those divs:
let string = '';
items.forEach(item => string += '<div>'+ item.description + '</div>');
The string c is not modified in place, but returns a new string.
Use c = c.concat(key + ': ' + jsObj2[key]);
You have to assign concated value to a variable.
var jsObj2 = {
"key1": "value3",
"key4": "value4"
};
var c = '';
for (var key in jsObj2) {
c = c.concat(key + ': ' + jsObj2[key]);
}
console.log(c);
Try creating a variable len set to char.length - 1, decrement len within for loop, set data-alphabet to char[len]
$("body").append('<div class="grid"><div class="grid__col--12 lesson blacktxt"></div></div>');
var char = "ابتثجحخدذرزسشصضطظعغفقكلمنهوي";
for (i = 0, len = char.length - 1; i < 28; i++, len--) {
var $sound = '<nav class="cell"><a class="pointer blacktxt" data-asound="'+ i +'"><i class="fa fa-volume-up"></i></a></nav>'
var $csound = '<nav class="cell"><input type="text" class="txtcenter" data-csound="'+ i +'" placeholder="'+ i +'" /></nav>'
var $newDiv = $('<header class="table lessonSec" data-alphabet="'+ char[len] +'"/>').html($sound + $csound)
$(".lesson").append( $newDiv )
}
body {
background: #7fb8ff;
}
/* Center DIVs Vertically and Horizontally */
.table {
display: table;
width: 100%;
height: 100%;
text-align: center;
margin: 1em 0;
}
.cell {
display: table-cell;
vertical-align: middle;
}
/* Global Misc Classes */
.pointer {
cursor: pointer;
}
.txtcenter {
text-align: center;
}
<link href="http://treehouse-code-samples.s3.amazonaws.com/poly/css/application.css" rel="stylesheet"/>
<link href="https://fortawesome.github.io/Font-Awesome/assets/font-awesome/css/font-awesome.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Consider this snippet:
var chars = "ابتثجحخدذرزسشصضطظعغفقكلمنهوي";
var arrayOfChars = chars.split('').reverse(); // got them in reverse order
var divs = arrayOfChars.map(buildDiv)
$('.mydiv').prepend(divs)
function buildDiv(c) {
return $('<div />').attr('data-around', c)
}
You set
examplestring = ""
inside the loop, so you empty it, then you add "foo", then you empty it, add another foo, empty it add a foo and get only one foo back. You want to declare the variable outside off the loop, before the loop runs.
Put var examplestring outside of loop
var number = 3
var examplestring = "";
for (var i = 0; i < number; i++) {
examplestring += "foo ";
}