I have a bit of code that requires printing underscores but to the line above it, how would i do this? I'm not sure how to print the underscore to the previous line, not much experience with javascript. thanks!
var landscape = function() {
var result = "";
var flat = function(size) {
for (var count = 0; count < size; count++)
result += "_";
};
var hill = function(size) {
result += " /";
for (var count = 0; count < size; count++)
result += ""+
"_";
result += " \\";
};
//BUILD SCRIPT
flat(3)
hill(4);
flat(6);
hill(1);
flat(1);
//END SCRIPT
return result;
it makes this ___ /____ \______ /_ \_`enter code here`enter code here`
and i want this
_____ ___
___/ \__/ \____/\_
You can keep track of the two lines separately and then concatenate them just before returning the result.
JS:
function landscape() {
var resultTop = '';
var resultBottom = '';
function hill(size) {
resultTop += ' ';
resultBottom += '/';
for (var i = 0; i < size; i++) {
resultTop += '_';
resultBottom += ' ';
}
resultTop += ' ';
resultBottom += '\\';
}
function flat(size) {
for (var i = 0; i < size; i++) {
resultTop += ' ';
resultBottom += '_';
}
}
flat(3);
hill(4);
flat(6);
hill(1);
flat(1);
var result = resultTop + '<br/>' + resultBottom;
return result;
}
Here's a fiddle.
One workaround is to print a unicode character that draws a line on top. Turns out there is such a character: the Upper One-eighth Block
It's "\u2594" in unicode escape or ▔ as HTML entity or you can simply copy/paste the literal character from the example below:
____/▔▔▔▔\____/▔▔\___
Related
I would like to apply same piece of code to two objects in JavaScript.
When calling getElementsByClass ,there appears 2 objects in my website.So I would like to apply the same code for both of them.Currently I'm applying it to only one Object (text[0]) and I would like to implement it also to text[1] .
var text=document.getElementsByClassName("th");
var text =text[0];
var newDom = '';
var animationDelay = 6;
for(let i = 0; i < text.innerText.length; i++)
{
newDom += '<span class="char">' + (text.innerText[i] == ' ' ? ' ' : text.innerText[i])+ '</span>';
}
text.innerHTML = newDom;
var length = text.children.length;
for(let i = 0; i < length; i++)
{
text.children[i].style['animation-delay'] = animationDelay * i + 'ms';
}
}
I think you want to do the same thing with using item[0] and item[1] together.
You can create a function. Or call this function by iterating your items too.
var text=document.getElementsByClassName("th");
function myFunc(text) {
var newDom = '';
var animationDelay = 6;
for(let i = 0; i < text.innerText.length; i++)
{
newDom += '<span class="char">' + (text.innerText[i] == ' ' ? ' ' : text.innerText[i])+ '</span>';
}
text.innerHTML = newDom;
var length = text.children.length;
for(let i = 0; i < length; i++)
{
text.children[i].style['animation-delay'] = animationDelay * i + 'ms';
}
}
}
myFunc(text[0]); // call functions with your items.
myFunc(text[1]);
Please help, I can't implement this javascript to my Blogger...
(function() {
var pre = document.getElementsByTagName('pre'),
pl = pre.length;
for (var i = 0; i < pl; i++) {
pre[i].innerHTML = '<span class="line-number"></span>' + pre[i].innerHTML + '<span class="cl"></span>';
var num = pre[i].innerHTML.split(/\n/).length;
for (var j = 0; j < num; j++) {
var line_num = pre[i].getElementsByTagName('span')[0];
line_num.innerHTML += '<span>' + (j + 1) + '</span>';
}
}
})();
You can see this Javascript work fine here : http://jsfiddle.net/tovic/AbpRD/1/
If you are seeing that following type of error when you try to add this JavaScript snippet in your theme code -
Error parsing XML: The content of elements must consist of well-formed character data or markup.
Then to resolve this error, use any of the following methods -
1. Wrap the code within a CDATA directive in the script tag. The code will look like -
<script>
//<![CDATA[
(function() {
var pre = document.getElementsByTagName('pre'),
pl = pre.length;
for (var i = 0; i < pl; i++) {
pre[i].innerHTML = '<span class="line-number"></span>' + pre[i].innerHTML + '<span class="cl"></span>';
var num = pre[i].innerHTML.split(/\n/).length;
for (var j = 0; j < num; j++) {
var line_num = pre[i].getElementsByTagName('span')[0];
line_num.innerHTML += '<span>' + (j + 1) + '</span>';
}
}
})();
//]]>
</script>
The only downside of this approach being that Blogger XML parser will ignore any data layout tags (like for example <data:blog.homepageUrl/>) within the CDATA directive. Rather than replacing them with their actual values, it will not interpret them and show them as is.
2. Escape the following characters in your code -
" is replaced with "
& is replaced with &
< is replaced with <
> is replaced with >
After escaping, the code should look like -
<script>
(function() {
var pre = document.getElementsByTagName('pre'),
pl = pre.length;
for (var i = 0; i & lt; pl; i++) {
pre[i].innerHTML = '<span class="line-number"></span>' + pre[i].innerHTML + '<span class="cl"></span>';
var num = pre[i].innerHTML.split(/\n/).length;
for (var j = 0; j & lt; num; j++) {
var line_num = pre[i].getElementsByTagName('span')[0];
line_num.innerHTML += '<span>' + (j + 1) + '</span>';
}
}
})();
</script>
The data layout tags will remain functional when following this method. Remember to not escape <> surrounding the data layout tag (aka <data:blog.homepageUrl/> will work but not <data:blog.homepageUrl/>)
3. If no data layout tags have to be included in the JavaScript. Then you can add it in an HTML/JavaScript gadget via the Layout tab instead of directly including it in the theme code.
How can i make ASCII send what is typed than code.
I mean if A is type on a keyboard the result on php should be the letter typed and not the Code
var ucode = function(s) {
var len = s.length;
var rs = "";
for ( var i = 0; i < len; i++) {
var k = s.substring(i, i + 1);
rs += "$" + (s.charCodeAt(i) + "1") + ";";
}
return rs;
};
I have a two-dimensional array (affiliates) where each array item consists of an image URL and a link URL. randArray is simply a generated list of random numbers (non-repeating) that matches the length of affiliates.
I've gotten to the point where I can successfully generate an HTML string for randomized image/url combinations.
(The above might be more information than needed but I wanted to provide a little background.)
What I'm trying to do is group every three iterations together and wrap in <p> tags. I've tried doing a nested loop inside the current one but could only get to where it was wrapping three of the same line in a <p> tag, whereas I need them to be three successive lines. I feel like this should be pretty easy, but I'm stumped. Any help would be much appreciated.
for(var y=0; y < randArray.length; y++) {
var image = affiliates[randArray[y]][0];
var url = affiliates[randArray[y]][1];
var fullString = '<img src="' + image + '">';
console.log(fullString);
}
WIthin your loop, y % 3 == 0 will be true on the first iteration and every third after it, so output your <p> when y % 3 == 0 and your </p> when y % 3 == 2 and after your loop if length % 3 != 0.
But a nested loop is probably easier to do and maintain:
var x, image, url;
var fullString = "";
var y = 0;
while (y < randArray.length) {
fullString += '<p>';
for (x = 0; x < 3 && y < randArray.length; ++x, ++y) {
image = affiliates[randArray[y]][0];
url = affiliates[randArray[y]][1];
fullString += '<img src="' + image + '">';
}
fullString += '</p>';
}
for(var y=0; y < randArray.length; y++) {
var image = affiliates[randArray[y]][0];
var url = affiliates[randArray[y]][1];
var fullString = '';
if(y%3 == 2){
fullString += '<p>';
}
fullString = '<img src="' + image + '">';
if(y%3 == 2){
fullString += '</p>';
}
console.log(fullString);
}
I am not sure if I understand, but...
var t = [];
for(var i=0;i<affiliates.length;i++) t.push(i);
while(t.length>2){
var str = '';
for(var i=0;i<3;i++){
str += '<a href="';
var r = Math.floor(Math.random()*t.length);
str += affiliates[t[r]][1] + '"><img src="';
str += affiliates[t[r]][0] + '"></a>';
t.splice(r,1);
}
str = '<p>'+str+'</p>';
console.log(str);
}
I would do something like this, but it's not optimized code, sorry :/
var counter = 1; //external counter for module (%3)
var open = false;
var tagOpen = '<p>';
var tagClose = '</p>';
var fullstring = '';
var buffer = ''
for(var y=0; y < randArray.length; y++) {
var image = affiliates[randArray[y]][0];
var url = affiliates[randArray[y]][1];
//groups entries
buffer += '<img src="' + image + '">';
if(counter % 3 == 0){
fullstring = tagOpen + buffer + tagClose; //concat strings
console.log(fullString);
open = false; //set open to false
} else {
open = true;
}
counter++;
}
//if at the end of the loop there's still an open tag, close it
if(open){
fullstring += tagClose;
}
I have an array made up of strings separated by "|":
LOG: 380|2|choice|Question_8_13240_1351170102936|||incorrect|2012-10-25T15:01:13|1|<ln>380|2|choice|Question_10_13280_1351170102995|||incorrect|2012-10-25T15:01:17|1|
and need to get Question_8_13240_1351170102936 from each string so that i can see if there are any dupicate questions thus the string after the 3rd occurence of "|" and before the 4th
the code that build the array:
AssessmentManager.prototype.submit = function () {
assessData = "";
assessmentItemsLength = this.assessmentItems.length;
for (i = 0; i < this.assessmentItems.length; i++) {
if (!this.assessmentItems[i].getSubmitted()) {
assessmentFlatDataItem = this.assessmentItems[i].getFlatData();
if (assessmentFlatDataItem.length > 0) {
if (assessData.length > 0)
assessData += "<ln>";
assessData += assessmentFlatDataItem;
}
}
}
}
then GetFlatdata:
AssessmentItem.prototype.getFlatData = function () {
result = _userScormModuleId + "|";
if (this._interactionIdx)
result += this._interactionIdx;
result += "|";
if (this._interactionType)
result += this._interactionType
result += "|";
if (this._interactionId)
result += this._interactionId
result += "|";
if (this._learnerResponse)
result += this._learnerResponse
result += "|";
if (this._correctResponse)
result += this._correctResponse
result += "|";
if (this._result)
result += this._result
result += "|";
if (this._timeStamp)
result += this._timeStamp
result += "|";
if (this._weighting)
result += this._weighting
result += "|";
if (this._description)
result += this._description
return result;
}
how will i achieve this?
thanks
i have this now:
learnerResponsesArray.push({
key: j,
value: this.assessmentItems.split('<ln>')
});
then loop through learnerResponsesArray?
var cells = mystring.split("|");
// nth item...
var myitem = cells[n];
but as #mori57 said, why use this format instead of JSON?
Try this
str = "LOG: 380|2|choice|Question_8_13240_1351170102936|||incorrect|2012-10-25T15:01:13|1|<ln>380|2|choice|Question_10_13280_1351170102995|||incorrect|2012-10-25T15:01:17|1| "
console.log(str.split(":")[1].split("|")[3])
If you need the value between the 3d and 4th |, you could use:
var fourthelement = [yourstring].split(/\|/,4).pop();