Skip to content

Instantly share code, notes, and snippets.

@umarov
Last active June 21, 2017 04:39
Show Gist options
  • Save umarov/6992f3d364f3c78529ac6aa1fcc092fd to your computer and use it in GitHub Desktop.
Save umarov/6992f3d364f3c78529ac6aa1fcc092fd to your computer and use it in GitHub Desktop.
Simple JS Examples
let cities = ['Alexandria', 'San Francisco', 'Los Angeles', 'New York', 'Toronto'];
function twoWordedWithForLoop(sentences) {
let twoWordedSentences = []
for(let index = 0; index < sentences.length; index++) {
let currentSentence = sentences[index]; // String
let splittedSentence = currentSentence.split(" "); // Array
let numberOfWords = splittedSentence.length // Number
if (numberOfWords === 2) {
twoWordedSentences.push(currentSentence)
}
}
return twoWordedSentences;
}
function twoWordedWithWhileLoop(sentences) {
let twoWordedSentences = []
let sentencesIterator = sentences.entries() // Array Iterator
let currentSentenceIterator = sentencesIterator.next(); // Object
while(!currentSentenceIterator.done) {
let currentSentence = currentSentenceIterator.value[1]; // String
let splittedSentence = currentSentence.split(" "); // Array
let numberOfWords = splittedSentence.length // Number
console.log(currentSentence, splittedSentence, numberOfWords)
if (numberOfWords === 2) {
twoWordedSentences.push(currentSentence)
}
currentSentenceIterator = sentencesIterator.next();
}
return twoWordedSentences;
}
function map(forEachItem) {
for(let index = 0; index < sentences.length; index++) {
forEachItem(sentences[index])
}
}
cities.filter((city) => city.split(" ").length === 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment