var object = {
mastermind : "Brain",
henchman: "Pinky",
battleCry: function (noOfBrains) {
return "They are " + this.henchman + " and the" +
Array(noOfBrains + 1).join(" " + this.mastermind);
}
};
var battleCry = object.battleCry(4);
--
Can you explain why array().join() return:
Correct: "They are Pinky and the Brain Brain Brain Brain"
Incorrect: "They are Pinky and the 5 Brain"
The array().join() is used to join all the elements of an array together in one string. Optionally, you can specify a delimiter to separate the elements.
In your code, Array(noOfBrains + 1) creates a new array with the given length, in your example 4 + 1 = 5 items, but all those 5 items are empty because your code doesn't assign anything to them. So your code is joining 5 empty strings using " Brain" as delimiter. Essentially, this function is actually a repeater of any string assigned to the mastermind property for as many times as you pass it.
Because in Array(noOfBrains + 1).join(" " + this.mastermind) this.mastermind contains the string "Brain". and noOfBrains is '4'. so it becomes Array(4+1).join(" " +"Brain") into Array(5).join(" Brain") will show the output as Brain Brain Brain Brain
This creates an array of a given length with undefined elements. In this example length of 5. When 5 elements are joined together there are 4 delimiters and each is delimiter is " Brain"
var dummyArray = Array(5);
console.log("Our Dummy Array");
console.log(dummyArray);
console.log();
console.log("Our Dummy Array Joined More Conventially");
console.log(dummyArray.join(","));
console.log();
console.log("Our Dummy Array Joined More Brainly");
console.log(dummyArray.join(" Brain"));
console.log();
Array join joins all elements of array to string using some string glue.
var a = ['one', 'two', 'three'];
console.log(a.join(', '));
You will see "one, two, three" in the console.
In your case glue is " Brain". Array is [5] - i mean array with one element which is 5 (4 + 1 = 5). So, [5].join(' Brain') = "5"
If you need repeat some string 2 or more times, you can use String.repeat(times).
In your case:
var bcObject = {
mastermind : "Brain",
henchman: "Pinky",
battleCry: function (noOfBrains) {
return "They are " + this.henchman + " and the" +
(' ' + this.mastermind).repeat(noOfBrains);
}
};
var battleCry = bcObject.battleCry(4);
Related
Assume i have a string
var str = " 1, 'hello' "
I'm trying to give a function the above values found in str but as integer and string- not as one string-
for example myFunc(1,'hello')
how can i achieve that
i tried using eval(str),
but I'm getting invalid token ,
How can i solve this?
The following should work with any number of arguments.
function foo(num, str) {
console.log(num, str);
}
const input = "1, 'hel,lo'";
const args = JSON.parse('[' + input.replace(/'/g, '"') + ']');
foo(...args);
You've almost got the right idea with eval(str) however, that isn't the thing you actually want to evaluate. If you do use eval(str), it is the same as saying eval(" 1, 'hello' ")
However, what you really want to do is:
eval("func(1, 'hello world')).
To do this you can do:
eval(func.name + '(' + str.trim() + ')');
Here we have:
func.name: The name of the function to call. You can of course hard code this. (ie just write "func(" + ...)
str.trim(): The arguments you want to pass into the given function. Here I also used .trim() to remove any additional whitespace around the string.
Take a look at the snippet below. Here I have basically written out the above line of code, however, I have used some intermediate variables to help spell out how exactly this works:
function func(myNum, myStr) {
console.log(myNum*2, myStr);
}
let str = " 1, 'hello, world'";
// Build the components for the eval:
let fncName = func.name;
let args = str.trim();
let fncStr = fncName + '(' + args + ')';
eval(fncStr);
Alternatively, if you only wish to pass in two arguments you can use .split(',') on your string to split the string based on the comma character ,.
Using split on " 1, 'hello' " will give you an array such as this one a:
let a = [" 1", "'hello'"];
Then cast your string to an integer and remove the additional quotes around your string by using .replace(/'/g, ''); (replace all ' quotes with nothing ''):
let numb = +a[0].trim(); // Get the number (convert it to integer using +)
let str = a[1].trim().replace(/'/g, ''); // get the string remove whitespace and ' around it using trim() and replace()
Now you can call your function using these two variables:
func(numb, str);
function func(myNum, myStr) {
console.log('The number times 2 is:', myNum*2, "My string is:", myStr);
}
let arguments = " 1, 'hello' ";
let arr = arguments.split(',');
let numb = +arr[0].trim(); // Argument 1
let str = arr[1].trim().replace(/'/g, ''); // Argument 2
func(numb, str);
Having a list (array) of tags ['tag1', 'tag2', 'tag3'] I want to generate a nice title like: Content tagged tag1, tag2 and tag3.
For the moment I have:
"Content tagged " + tags_titles.join(" and ");
with the result:
Content tagged tag1 and tag2 and tag3
I know it's a simple question, but I am curious if there is a nice solution for this case.
You could get the two last elements and join them with ' and ' and put it as last element back into the array and later join all elements with ', ' for getting a nice string.
Methods
Array#concat, joins two arrays and returns a new array
Array#splice, for getting the last two elemensts of the array
Array#join, joins an array with the given spacer.
This proposal works for any length of an array, even with one or two elements.
function nice([...array]) {
return array.concat(array.splice(-2, 2).join(' and ')).join(', ');
}
console.log("Content tagged " + nice(['tag1']));
console.log("Content tagged " + nice(['tag1', 'tag2']));
console.log("Content tagged " + nice(['tag1', 'tag2', 'tag3']));
A bit of slicing and dicing using Array.prototype.slice:
function naturalLanguageJoin(arr){
if(!arr)
return '';
if(arr.length<2)
return (arr.length>0) ? arr[0] : '';
return arr.slice(0,arr.length-1).join(", ") + " and " + arr[arr.length-1];
}
console.log(naturalLanguageJoin(['tag1', 'tag2', 'tag3']));
console.log(naturalLanguageJoin(['tag1', 'tag2']));
console.log(naturalLanguageJoin(['tag1']));
console.log(naturalLanguageJoin([]));
console.log(naturalLanguageJoin(null));
An array can be treated as a stack so you can pop the last element and in turn write this
var last = tags_titles.pop();
last = tags_titles.length ? ` and ${last}` : last;
`Content tagged ${tags_titles.join(", ")} ${last}`
The code uses ES6 string templates, which I generally find to be more readable than doing string concatenation in the code. It also utilizes the fact that the pop method essentially performs to operations. Gets the lasst element of the array and mutate the array. That eliminate the need to do the mutation explicitly (using slice)
Try like this
var g = ['tag1', 'tag2', 'tag3'];
var title = g.slice(0, g.length-1).join(',').concat(' and ').concat(g[g.length-1]);
From my view, This is an simple approach
var tags = ['tag1', 'tag2', 'tag3'];
console.log("Content tagged " + tags.slice(0, -1).join(', ')+' and '+tags.slice(-1));
Hope it helps you :)
JsFiddle
Try this,
var arr = ['tag1', 'tag2', 'tag3'];
var lastStr = arr.pop();
var str = "Content tagged " + arr.join(", ") + " and " + lastStr;
Something like this would be my approach
function arrayFormat(arr) {
var output = arr.splice(0, arr.length - 1).join(", ");
output += " and " + arr[0];
return output;
}
console.log(arrayFormat(["a", "b", "c"]));
Having a list (array) of tags ['tag1', 'tag2', 'tag3'] I want to generate a nice title like: Content tagged tag1, tag2 and tag3.
For the moment I have:
"Content tagged " + tags_titles.join(" and ");
with the result:
Content tagged tag1 and tag2 and tag3
I know it's a simple question, but I am curious if there is a nice solution for this case.
You could get the two last elements and join them with ' and ' and put it as last element back into the array and later join all elements with ', ' for getting a nice string.
Methods
Array#concat, joins two arrays and returns a new array
Array#splice, for getting the last two elemensts of the array
Array#join, joins an array with the given spacer.
This proposal works for any length of an array, even with one or two elements.
function nice([...array]) {
return array.concat(array.splice(-2, 2).join(' and ')).join(', ');
}
console.log("Content tagged " + nice(['tag1']));
console.log("Content tagged " + nice(['tag1', 'tag2']));
console.log("Content tagged " + nice(['tag1', 'tag2', 'tag3']));
A bit of slicing and dicing using Array.prototype.slice:
function naturalLanguageJoin(arr){
if(!arr)
return '';
if(arr.length<2)
return (arr.length>0) ? arr[0] : '';
return arr.slice(0,arr.length-1).join(", ") + " and " + arr[arr.length-1];
}
console.log(naturalLanguageJoin(['tag1', 'tag2', 'tag3']));
console.log(naturalLanguageJoin(['tag1', 'tag2']));
console.log(naturalLanguageJoin(['tag1']));
console.log(naturalLanguageJoin([]));
console.log(naturalLanguageJoin(null));
An array can be treated as a stack so you can pop the last element and in turn write this
var last = tags_titles.pop();
last = tags_titles.length ? ` and ${last}` : last;
`Content tagged ${tags_titles.join(", ")} ${last}`
The code uses ES6 string templates, which I generally find to be more readable than doing string concatenation in the code. It also utilizes the fact that the pop method essentially performs to operations. Gets the lasst element of the array and mutate the array. That eliminate the need to do the mutation explicitly (using slice)
Try like this
var g = ['tag1', 'tag2', 'tag3'];
var title = g.slice(0, g.length-1).join(',').concat(' and ').concat(g[g.length-1]);
From my view, This is an simple approach
var tags = ['tag1', 'tag2', 'tag3'];
console.log("Content tagged " + tags.slice(0, -1).join(', ')+' and '+tags.slice(-1));
Hope it helps you :)
JsFiddle
Try this,
var arr = ['tag1', 'tag2', 'tag3'];
var lastStr = arr.pop();
var str = "Content tagged " + arr.join(", ") + " and " + lastStr;
Something like this would be my approach
function arrayFormat(arr) {
var output = arr.splice(0, arr.length - 1).join(", ");
output += " and " + arr[0];
return output;
}
console.log(arrayFormat(["a", "b", "c"]));
string1 = prompt("String 1?");
string2 = prompt("String 2?");
if (string1.length == string2.length)
alert (string1 + " and " + string2 + " : are identical in size.")
else
alert (string1 + " and " + string2 + " : not identical in size.")
for(i=0; i<string1.length; i++)
{
for(j=0; j<string2.length; j++)
{
if (string1.charAt[i] == string2.charAt[i])
alert (string1.charAt[i] + " and " + string2.charAt[j] + " : are identical values.")
else
alert (string1.charAt[i] + " and " + string2.charAt[j] + " : are non-identical values.")
}
};
The second part of the code keeps returning "undefined" and "undefined". I'm trying to compare both strings to see if they hold identical values. Would appreciate any help!
You are using the wrong index when you compare the characters. This:
if (string1.charAt[i] == string2.charAt[i])
should be:
if (string1.charAt(i) == string2.charAt(j))
Besides using parantheses, as Rob W pointed out, you should use the variable j to access the character in string2.
I'm not sure why you are comparing each character in one string with every character in the other string, though... If you actually want to compare each character to the corresponding character in the other string, then you should not have nested loops but a single loop.
Use charAt(i) instead of charAt[i] (parentheses instead of square braces).charAt is a String method, not a string's property.
Equivalent methods:
string1[i]
string1.charAt(i)
string1.substr(i, 1)
string.substring(i, i+1)
Wtf?
string1 === string2
Why not?
What I'm looking to do is split data from string into an array.
Here's the general idea of the text format...
xxxxx denotes any mix of alpha-numeric-whitespace data.
xxxxx
1 xxxxxxxxxx
2 xxxxxxxxxx
xxxxxxxxx
xxxxxxxxx
xxxxxxxx
3 xxxxxxxxxx
4 xxxxxxxxxx
xxxxxxxxxx
5 xxxxxxxxxx
(When numbers get into the double digits, the ten's place goes into the blank position in-front of the number)
Now what I want to do is have an array of 5 elements (in this case), which stores the number and all data that trails (including the new lines). In the past this was not a big deal and I could use string.split("\n") , but now I need to delimit based on some sort of regex like /\n [0-9]{1,2}/ so I'm looking for a quick and easy way to do this (as split() doesn't support regex).
I want the array to be like
array[1] = " 1 xxxxxxxxxx"
array[2] = " 2 xxxxxxxxxxx\nxxxxxxxxxx\nxxxxxxxxxx"
array[3] = " 3 xxxxxxxxxx"
...etc
split() does support regexes. Try this:
text.split(/\n(?=[1-9 ][0-9] )/)
You can use lookahead and split on (?= [1-9] |[1-9][0-9] ), perhaps anchored at the beginning of a line, but there may be issues with ambiguities in the xxxx part. This also doesn't ensure that the numbering is sequential.
Example
var text =
"preface\n" +
" 1 intro\n" +
" 2 body\n" +
"more body\n" +
" 3 stuff\n" +
"more stuff\n" +
"even 4 stuff\n" +
"10 conclusion\n" +
"13 appendix\n";
print(text.split(/^(?= [1-9] |[1-9][0-9] )/m));
The output is (as seen on ideone.com):
preface
, 1 intro
, 2 body
more body
, 3 stuff
more stuff
even 4 stuff
,10 conclusion
,13 appendix
As #polygenelubricants said, you could use a regex with replace and make an interim delimiter, then split on that delimiter and remove it.
Here is a working example from the string you gave above and another I made to test the function. It works with both. Since you didn't provide any real data for an example, I can't test that, but hopefully this will at least get you going on the right track.
function SplitCrazyString(str) {
var regex = /(\n\s?\d+\s[^(\n\s?\d+)]+)/mg;
var tempStr = str.replace(regex, "~$1");
var ary = tempStr.split('~');
for (var i = 0; i < ary.length; i++) {
ary[i].replace('~', '');
}
return ary;
}
var x = "xxxxx\n" +
" 1 xxxxxxxxxx\n" +
" 2 xxxxxxxxxx\n" +
"xxxxxxxxx\n" +
"xxxxxxxxx\n" +
"xxxxxxxx\n" +
" 3 xxxxxxxxxx\n" +
" 4 xxxxxxxxxx\n" +
"xxxxxxxxxx\n" +
" 5 xxxxxxxxxx\n";
var testStr = "6daf sdf84 as96\n" +
" 1 sfs 4a8dfa sf4asf\n" +
" 2 s85 d418 df4 89 f8f\n" +
"65a1 sdfa48 asdf61\n" +
"w1c 987a w1ec\n" +
"a6s85 d1a6f 81sf\n" +
" 3 woi567 34ewn23 5cwe6\n" +
" 4 s6k 8hf6 9gd\n" +
"axxm4x1 dsf615g9 8asdf1jt gsdf8as\n" +
" 5 n389h c8j923hdha 8h3x982qh\n";
var xAry = SplitCrazyString(x);
var testAry = SplitCrazyString(testStr);