Videos
I'm doing a code wars practice on JavaScript involving the JavaScript methods split(), concat(), and join(). This is the problem:
Implement a function which accepts 2 arguments: string and separator.
The expected algorithm: split the string into words by spaces, split each word into separate characters and join them back with the specified separator, join all the resulting "words" back into a sentence with spaces.
For example:
splitAndMerge("My name is John", " ") == "M y n a m e i s J o h n" splitAndMerge("My name is John", "-") == "M-y n-a-m-e i-s J-o-h-n" splitAndMerge("Hello World!", ".") == "H.e.l.l.o W.o.r.l.d.!" splitAndMerge("Hello World!", ",") == "H,e,l,l,o W,o,r,l,d,!"This is the code I've made thus far:
function splitAndMerge(string, separator) {
let char = string;
let cap = char.split("");
let combo = cap.join(" ");
return char.split("").join(" ");
}It splits it up but I don't know how to get it to produce the final results of having each separator be different. Can anyone help?
With JavaScript’s String.prototype.split function:
var input = 'john smith~123 Street~Apt 4~New York~NY~12345';
var fields = input.split('~');
var name = fields[0];
var street = fields[1];
// etc.
According to ECMAScript6 ES6, the clean way is destructuring arrays:
const input = 'john smith~123 Street~Apt 4~New York~NY~12345';
const [name, street, unit, city, state, zip] = input.split('~');
console.log(name); // john smith
console.log(street); // 123 Street
console.log(unit); // Apt 4
console.log(city); // New York
console.log(state); // NY
console.log(zip); // 12345
You may have extra items in the input string. In this case, you can use rest operator to get an array for the rest or just ignore them:
const input = 'john smith~123 Street~Apt 4~New York~NY~12345';
const [name, street, ...others] = input.split('~');
console.log(name); // john smith
console.log(street); // 123 Street
console.log(others); // ["Apt 4", "New York", "NY", "12345"]
I supposed a read-only reference for values and used the const declaration.
Enjoy ES6!
There are several functions in JavaScript which can be used to split a string.
1. Split - (Split a string into an array of substrings)
var str = "This is a string";
var str = "This is a string";
var res = str.split(" ");
console.log(res);
It is important that split gives back an array with the string chunks.
2. Substr - (Extract parts of a string)
var str = "This is a string";
var res = str.substr(1, 4);
console.log(res);
3. Substring - (Extract characters from a string)
var str = "This is a string";
var res = str.substring(1, 4);
console.log(res);
Reference
W3Schools Split
W3Schools Substr
W3Schools Substring
I think you want to use substring() (read more here: https://www.w3schools.com/jsref/jsref_substring.asp)
For example,
var str = "Hello world!";
var res = str.substring(1, 4);
would be equivalent to this Python code:
str = "Hello world!"
res = str[1:4]
res would be 'ell'. The start and end indices work the same as in Python (includes the start index, goes through but not including the end index.