Dynamically create multiline string in JavaScript [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
This is my first interaction ever with JavaScript so pls bear with me on this.
I want to dynamically create a multiline variable (string). The way it is going to be created based on some conditionals, something like
if (${{ inputs.a_gha_input }}) {
add_this_line_to_a_multiline_js_var("Hello")
}
if (${{ inputs.another_gha_input }}) {
add_this_line_to_a_multiline_js_var("World")
}
I would like my end result to be something like this
Hello
World
(in case both conditions are true of course)
Thanks

Just use carriage return symbols in your multiline string. Your function add_this_line_to_a_multiline_js_var(line) may look something like this:
let multilineString = '';
function addLineToMultilineString(line) {
multilineString += (line + '\n');
}

Related

How to Display each variable of for loop in a div tag using javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to Display each variable of for loop in a div tag using javascript? Using innerHTML i am only able to display the last variable of for loop
You can do as follows, but it is better to use lists.
Lists: https://developer.mozilla.org/pt-BR/docs/Web/HTML/Element/ul
const myDreams = ["Fly", "Be rich", "Go to the moon"];
const myDreamsDiv = document.querySelector("#myDreams");
//The secret here is to add a breakline(<br>) between each word.
myDreams.forEach(dream => myDreamsDiv.innerHTML += (dream + "<br>"));
<div id="myDreams"></div>

Is there a way to split a string separated by lines, into a 2D Array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am a beginner to javascript and I am confused on how to split a string which is separated by line breaks, into a 2d array. Here is the string. The numbers represent something that I need to make the code function, so excuse that:
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
20000000010000000000000100000001000001000001000000000001000000000100010000010000010000000100001
11011111110111111111110101110101011101011101011101111101111111010101011101010101110101111100101
11000000010000010001010001000100010001000100000101000100000100010101000101010100000100000000101
11011111011111010101011111011111011111011111111101010111110101110101110101110111111111111111101
11010001000001000101000001010001010000010000010001010001000101010100010001000100010001000100001
11010101111101111101110101010101010111110111010111011101011101010111011111011101010101011100101
11010100000000000100010101000100010000010001000101010001000001010100010001010001000100010000101
11010111111111110111010101111111011111011101111101010111111111010101010101010101111111110111101
11010100000000000001010100000001000001000100010000010000010000000001010100010101000000000100001
11010111011111111101010111011111110111110111011111011111010111111111010111110111011111111111111
11010001000100010001010101010000010100010001000001010001000101000001010000000100010000000000001
11011101110101010111010101010101010101010111111101011101111101011101110111111101110111111111101
11000101000101010001010001000101010001000100000001000001000001010100000100000001000100000100001
11011101111101011111011111111101111111111101111111111101110101010111111101111101011101110101111
11010001000001010000010000000100000100000001000001000100000101010000000100010001000101000100001
11010111011111010111110111110111110101111111010111010111111101010111110111011111110111011111001
11010001010001000100000100000000000101000000010001010001000001000101000101000100010001000100001
11011101011101111111110111111111110101011111111101011101011111111101011101110101011101110101001
11010101000100000000010000000100000101000000000101000100000000010001000100010001000000000101001
11010101110101111111011111110111111101111111111101110101111101110101110111011111111111111101001
11000100010100010001000000000100000001000000000100010101000101000101000100010100000001000001001
11110101110100010111111111011101111111011111110101110101010111011101011101010101011101011111111
11000101000100010100000001010001000000010001000101000101010000000100000001010001000101000000001
11011101011101110101011111010111011111110111011101011101011111111111111111010111110111111111001
11000100010001000101000001010001000000010100010100000001010001000001000001010100000100010001001
11110111110101110101111101011101111111010101110101111111010111011101010111010101110101010101001
11000100010100010100010101000100000001000101000001010001000100010101010001010100010101010100001
11011101010111010111010101110111111101110101111101010101011101110101111101011111010101010111111
11000101000100010000010100000000000100000100000100010100010001010001000100000000010101010000001
11110101111101011111010111111111111111111101110111010111110111010111010111111111110101011111001
11010100010001010001000001010000000001000100010100010001010100010100010000000000010101000000001
11010111010111110101111101010101111101010111110101111101010101110101011111111111011101111111001
11000100010100000101000000010101000100010000010100010000010101000101000001000001000100010001001
11011111110101111111011111110111010111111111010111110111110101011111111101011101110101010101001
11000000000100010000000100010000010100010001010000000100000101000000010001000100000101000101001
11111111110111011111011101010111110101010101011111110101111101111111010111111111011111011101001
11000000000101000001010001010100000101010100010000010100010001000000010000000001010001010001001
11011111111101111101110111011101111101010111110111011111010111011111111111111101010101110111001
11000000000000000100000100000001000001000000000100000000010000000000000000000001000100000100003
Hey buddy I'm also new but I actually have an idea. You can try this:
a.split("\n");

How to parse or loop this [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have tried everything I can think of to get the data but cannot figure this out.
The data is in a structure like this:
[{"code":1000,"day":"Sunny","night":"Clear","icon":113,"languages":
[{"lang_name":"Arabic","lang_iso":"ar","day_text":"مشمس","night_text":"صافي"}]
}]
I've tried looping, using key:value using the dot notation and bracket notation and cannot get the info.
I'm trying to get to the "languages" so I can parse them for the weather.
var arr = [{"code":1000,"day":"Sunny","night":"Clear","icon":113,"languages":
[{"lang_name":"Arabic","lang_iso":"ar","day_text":"مشمس","night_text":"صافي"}]
}]
arr.forEach(function(obj){ //loop array
obj.languages.forEach(function(language) { //loop languages
console.log(language); //language object
console.log(language.lang_name); //language property
});
});

Where to store return messages for javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Where should I store message types javascript function is returning? I can just return string messages, but it seems like a bad idea. Are there any best practices?
For example in this kind of function (of course real functions make more sense then this)
function isFooValid(foo){
if(foo.length < 4){
return "TOO_SHORT";
} else if(foo.length >100) {
return "TOO_LONG";
} else if(foo.charAt(1) == 'A'){
return "THERE_SHOULD_NOT_BE_SECOND_CHARACTER_A";
}
}
You can create a variable for that. For example you can have:
myMessages ={
"secondChar": "THERE_SHOULD_NOT_BE_SECOND_CHARACTER_A",
"tooShort": "TOO_SHORT"
}
Then you can retrieve them as myMessages.tooShort, for example.
Even, if you want to have something similar to the string resources in android, you can store all the strings in a json file. If you load it before you js script, you are going to have the myMessages var available.

How to "parse" js twice? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The following Code should output:
txt1txt2txt3...
var result ='';
var help='';
var a_1 = "txt1";
var a_2 = "txt2";
...
for (i=1;i<=100;i++)
{
help = 'a_' + i; // This line should be "parsed twice" !
result = result + help;
document.write(result);
}
Of course, I can avoid this by using the if-, or case-function for each "a_ i",
but this would make my js file very big.
So is there a way to parse this "help line" twice with js?
I asume, I could do it with including one js file in another, but I dont like this way?
No PHP , because the code should work clientside
Better to use an array.
eval() is the function for "double parsing" (but could be really dangerous !)

Categories