I am doing some homework problems using JavaScript. I need to do some string and array manipulation. I've solved similar problems in PHP using the explode
and implode
functions. Is there something similar that I can use in JavaScript?
Here is a sample of my code (simplified for this question). I'll use explode
and implode
even though they are not available in JS:
function getHashtags(str) {
let words = explode(' ', str);
let hashtags = [];
words.forEach((word) => {
if (word[0] === '#') {
hashtags.push(word);
}
});
}
function generateSentence() {
let subjects = ['He', 'She', 'John', 'Jane', 'The girl', 'The boy', 'The dog'];
let verbs = ['ran', 'jumped', 'ate', 'danced', 'played', 'laughed'];
let adverbs = ['quickly', 'happily', 'merrily', 'slowly'];
const random = (length) => Math.floor(Math.random()*length);
let result = [];
result.push(subjects[random(subjects.length)]);
result.push(verbs[random(verbs.length)]);
result.push(adverbs[random(adverbs.length)]);
return implode(' ', result) + '.';
}