Rename js snippets

This commit is contained in:
Angelos Chalaris
2023-05-19 20:23:47 +03:00
parent 82a614e42e
commit 9d032ce05e
305 changed files with 70 additions and 70 deletions

View File

@ -0,0 +1,26 @@
---
title: Right substring generator
type: snippet
language: javascript
tags: [string,generator]
cover: boutique-home-office-2
author: chalarangelo
dateModified: 2022-07-25T05:00:00-04:00
---
Generates all right substrings of a given string.
- Use `String.prototype.length` to terminate early if the string is empty.
- Use a `for...in` loop and `String.prototype.slice()` to `yield` each substring of the given string, starting at the end.
```js
const rightSubstrGenerator = function* (str) {
if (!str.length) return;
for (let i in str) yield str.slice(-i - 1);
};
```
```js
[...rightSubstrGenerator('hello')];
// [ 'o', 'lo', 'llo', 'ello', 'hello' ]
```