Es6 comes in handy with the generators and the iterators.
Here is simple example of their usage.
given a number n, below is how to make the array with a iterable number range.
how to use the iterable numbers:
let arr = [...n]
The expression above create an array of from 0 to n.
// logic
- expand the Number primative and make it a iterable with Symbol.iterator.
- Attach a generator function that yields each value required.
- The iterable number can only be used in an array.
You can modify the function to create a desired range. eg an array of values less than n, or an array of values greater than n. Enjoy our challenges.
// logic
Number.prototype[Symbol.iterator] = function * (){
for (let i=0;i<=this;i++) {
yield i
}
}
// you can use this to create your own range
// TODO: Add your tests here
// Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework.
// [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework)
// are still available for now.
//
Number.prototype[Symbol.iterator] = function * (){
for (let i=0;i<=this;i++) {
yield i
}
}
// For new tests, using [Chai](https://chaijs.com/) is recommended.
// You can use it by requiring:
const assert = require("chai").assert;
// If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted.
describe("Solution", function() {
it("should test for something", function() {
assert.deepEqual([...5],[0,1,2,3,4,5]);
assert.deepEqual([...2,...3],[0,1,2,0,1,2,3] );
});
});
A pangram is a sentence using every letter of a given alphabet at least once.
Pangrams have been used to display typefaces, test equipment, and develop skills in handwriting, calligraphy, and keyboarding.
Below is a solution to the following problem
write a function that detects a pangram if a string is passed as an argument.
The function show return true or false
Constraints_
- input can contain non-word characters
- solution show be case insensitive
My solution and logic - use an array of alphabets in either Uppercase or Lowercase
- use the array.prototype.every and array.prototype.map method to check if the string(input) contains each letter once.
- Don't forget to remove all the non-word Characters from the input. And also remember to convert the input string to case corresponding to your alphabets.
- When you compare the characters with alphabet array using map. The map function return array true or false
if the entire map array contains true, the solution is valid. this is where the array.every function comes to play.
let isPangram =str=>'abcdefghijklmnopqrstuvwxyz'
.split('')// create an array of alphabets
.map(v=>str.replace(/\W/g,'')// map the array and check if the input contains all the letters in alphabet
// map function should return [true] for every right character
.toLowerCase().includes(v))
.every(v=>v===true)// if the mapped array contains all true values, then the str is a pangram else not.
// TODO: Add your tests here
// Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework.
// [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework)
// are still available for now.
//
// For new tests, using [Chai](https://chaijs.com/) is recommended.
// You can use it by requiring:
// const assert = require("chai").assert;
// If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted.
describe("Solution", function() {
it("should passed all test", function() {
Test.assertEquals(isPangram('Quick zephyrs blow, vexing daft Jim'),true);
Test.assertEquals(isPangram('Sphinx of black quartz, judge my vow'),true);
Test.assertEquals(isPangram('Two driven jocks help fax my big quiz'),true);
Test.assertEquals(isPangram(' Five quacking zephyrs jolt my wax bed'),true);
//A mad boxer shot a quick, gloved jab to the jaw of his dizzy opponent
Test.assertEquals(isPangram('A mad boxer shot a quick, gloved jab to the jaw of his dizzy opponent'),true);
Test.assertEquals(isPangram('Quick zephyrs blow, vexing daft '),false);
// assert.strictEqual(1 + 1, 2);
});
});
A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam, racecar.
Below its one line solution to this problem
funciton palindrome takes a string and check if its a palindrome or not. return true or false respectively.
/// here's the logic
- create an array from the string
- reverse the string and then compare it to the original string.
- A short One liner for you with es6.
let Palindrome =str=> str.split('').reverse().join('')===str
// TODO: Add your tests here
// Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework.
// [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework)
// are still available for now.
//
// For new tests, using [Chai](https://chaijs.com/) is recommended.
// You can use it by requiring:
const assert = require("chai").assert;
// If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted.
describe("Solution", function() {
it("should test for something", function() {
// Test.assertEquals(1 + 1, 2);
assert.strictEqual(Palindrome('racecar'),true);
assert.strictEqual(Palindrome('radar'),true);
assert.strictEqual(Palindrome('peep'),true);
assert.strictEqual(Palindrome('nitin'),true);
assert.strictEqual(Palindrome('civic'),true);
assert.strictEqual(Palindrome('276672'),true);//19791
assert.strictEqual(Palindrome('19791'),true)
assert.strictEqual(Palindrome('spencer'),false)
});
});