why eval() is not executing my code?
for (var i = 1; i <= 3; i++) {
str = "var foo_" + i + "_bar = " + i;
eval(str);
}
console.log(foo_1_bar);
console.log(foo_2_bar);
console.log(foo_3_bar);
The variable foo_2_bar is not declared at that iteration (i = 1). You need to put the console.log(...) outside of the loop.
I assume you're playing with js because eval is a little dangerous.
for (var i = 1; i <= 3; i++) {
str = "var foo_" + i + "_bar = " + i;
eval(str);
}
console.log(foo_1_bar);
console.log(foo_2_bar);
console.log(foo_3_bar);
Your logged variable doesn't exist
for (var i = 1; i <= 3; i++) {
str = "var foo_"+ i +"_bar = " + i;
eval(str);
console.log(foo_1_bar);
}
Related
The code works but I don't want the inner for loop to take me to the new line.
for (i = 0; i < 5; i++) {
for (j = 1; j <= i; j++) {
console.log('*');
}
console.log();
}
console.log('-----------------');
console.log will automatically break the line. Concatenate to a string instead of a log. Log at the end.
let str = '';
for(i = 0; i <= 5 ; i++) {
for(j = 1; j <= i; j++) {
str += '*';
}
str += '\n';
}
console.log(str);
You can do this way, with the help of a string variable:
for (i = 0; i < 5; i++) {
var str = '';
for (j = 1; j <= i; j++) {
str+='*';
}
console.log(str);
}
console.log('-----------------');
If you want to print at the page, use like below
for (i = 0; i < 5; i++) {
let j=0;
do{document.write("*");j++;}while(j < i)
document.write("<br/>")
}
You need to break the line with the console.log you can also controle the space between * with output+='*' + " ";
function pyramid() {
var total = 5;
var output="";
for (var i = 1; i <= total; i++) {
for (var j = 1; j <= i; j++) {
output+='*' + " ";
}
console.log(output);
output="";
}
}
pyramid()
You can get rid of second for loop as follows:
var str = '';
for (i = 1; i <= 5; i++) {
str +=Array(i).join("*");
str +='\n';
}
console.log(str);
let string = "";
for (let i = 0; i < 5; i++){
string += '*';
console.log(string);
}
Output:
*
**
***
****
*****
A simple way to solve this "exercise" in JavaScript:
let width = ""
while(width.length < 6) console.log(width += `#` );
Basically, we create a string (width) and increment its value using the while loop till we hit a restriction.
I found the more typical method "bulky"(?)...plus there's the issue of not getting the exact picture of a half pyramid.
let i,j
for (i= 0; i < 6; i++){
for (j = 0; j<=i; j++){
console.log("#")
}
console.log("\n")
}
function pyramid(n){
let result = "";
for(let i=0; i<=n; i++){
result += "*".repeat(i);
result += "\n"
}
return result;
}
console.log(pyramid(5));
//OutPut
*
**
***
****
*****
As we need n number of pyramid structure with '' / '#' / any symbol. by using above code we can achieve. Here you can see we just created a function called pyramid with one parameter 'n'. and inside function we declare a variable 'result'. So inside for loop the length of 'i' is "<=n" and also you can use "repeat() method to print '' 'i' times. So if you call that function like console.log(pyramid(5)); You can able to see your Answer as expected..
shortest code:
console.log('*\n**\n***\n****\n*****');
i am using javascript nested loops and i need to break second loop on some condition but only second loop not the first one can anybody tell me how is it possible
my code
var myVar = "";
var i, j;
loopone:
for (i = 0; i < 3; i++) {
text += "<br>" + "i = " + i + ", j = ";
looptwo:
for (j = 10; j < 15; j++) {
if (j === 12) {
break;
}
document.getElementById("demo").innerHTML = text += j + " ";
}
}
not this break statement stop both loops but i want to stop only second loop.
Any help would be appriciated. thanks in advance!
Well its so simple of you are using loops with label then you just have to mention the label(loop name) after break statement like this
var myVar = "";
var i, j;
loopone:
for (i = 0; i < 3; i++) {
text += "<br>" + "i = " + i + ", j = ";
looptwo:
for (j = 10; j < 15; j++) {
if (j === 12) {
break looptwo;
}
document.getElementById("demo").innerHTML = text += j + " ";
}
}
this will break only loop with name looptwo
So the goal is to have the following:
k_1_1
k_1_2
k_1_3
k_2_4
k_2_5
k_2_6
k_3_7
and so on
What I have so far is:
But I think I'm way off....
while (var qi = 0; qi <= 9; qi++ ) {
for(var ai = 0; ai <= 3; ai++ ) {
var thisName = "k_" + ai + "_" + qi;
var thisValue = "";
localStorage.setItem(thisName, thisValue);
}
}
Any help would be apreciated.
thanks
No need for two loops if you use a little ingenuity:
for (var i = 0; i <= 9; i++) {
var thisName = "k_" + (Math.floor(i/3)+1) + "_" + (i+1);
var thisValue = "";
localStorage.setItem(thisName, thisValue);
}
You're using a While loop with For loop arguments. While loops keep looping while a condition is TRUE. Why not switch it to a For loop like so (also, your qi and ai are switched):
for(var qi = 1; qi <= 9; qi++ ) {
for(var ai = 1; ai <= 3; ai++ ) {
var thisName = "k_" + qi + "_" + ai;
var thisValue = "";
localStorage.setItem(thisName, thisValue);
}
}
An alternate solution:
for (var ai = 1; ai <= (27) ; ai++ ) { // 27 was picked arbitrarily (3 * 9)
// set to what ever upper limit you need
// the ceiling of the current ai number divided by 3 will give you your qi
var qi = Math.ceil((ai/3));
//setting your values
var thisName = "k_" + qi + "_" + ai;
var thisValue = "";
//reporting
console.log(thisName, thisValue);
}
Here is my javascript code
for (var i = 1; i <= _MAXPAGECOUNT - 2; i++) {
e = document.getElementsByName("q" + i + "[]");
for (var j = 0; j <= e.length - 1; j++) {
if (e[j].checked) {
result = result + "," + i + ":" + e[j].value;
// break;
}
}}
The problem is this, it shows result like this 1:2,1:3,1:4,2:3,2:4,2:5
here in code i means question number and j means answer number, but I want to result as like this 1:2,3,4 ; 2:3,4,5
Try this
for (var i = 1; i <= _MAXPAGECOUNT - 2; i++) {
result = result+i+":";
e = document.getElementsByName("q" + i + "[]");
for (var j = 0; j <= e.length - 1; j++) {
if (e[j].checked) {
result = result + e[j].value;
// break;
}
}
if(i<_MAXPAGECOUNT - 2)
{
result = result+" ; ";
}
}
The title plus the following example are self-explanatory of what I don't achieve :-)
The idea is to replace something + counter in order to make it work.
for (var counter = 1; counter <= 6; counter++) {
var something + counter = $('element' + counter);
(something + counter).removeAttribute('class');
}
You could create an array, but much more simply:
for (var counter = 1; counter <= 6; counter++) {
$('element' + counter).removeAttribute('class');
}
Just do:
for (var counter = 1; counter <= 6; counter++) {
$('element' + counter).removeAttribute('class');
}
Unless you wanted to store it outside of the loop, in which case use an array.
Use an array.
var something = [];
for (var counter = 1; counter <= 6; counter++) {
something[counter] = $('element' + counter);
something[counter].removeAttribute('class');
}
Why can't you just get rid of the var altogether??
for (var counter = 1; counter <= 6; counter++) {
$('element' + counter).removeAttribute('class');
}
for (var counter = 1; counter <= 6; counter++) {
window[something + counter] = $('element' + counter);
window[something + counter].removeAttribute('class');
}
after that there will be a set of fields in window object, named something1, something2 etc (if something == "something", of course)