This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
numbers = [1, 2, 3, 4] | |
# Without the walrus operator | |
i = 0 | |
while i < len(numbers): | |
print(numbers[i]) | |
i += 1 | |
# With the walrus operator | |
while (i := 0) < len(numbers): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mostCommonWord(string) { | |
// Create a map to store the frequency of each word | |
const wordFrequency = new Map(); | |
// Split the string into an array of words | |
const words = string.split(' '); | |
// Iterate over the array of words | |
for (const word of words) { | |
// If the word is not in the map, add it with a frequency of 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Route::get('/users', 'UserController@index'); | |
class UserController extends Controller | |
{ | |
public function index() | |
{ | |
$users = User::all(); | |
return view('users.index', compact('users')); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const generateUniqueId = () => { | |
return Math.random().toString(36).substring(2) | |
} | |
generateUniqueId(); //7pvib9simcl | |
generateUniqueId(); //3aakkolzsu8 | |
generateUniqueId(); //ligt7dtz83h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ❌ | |
$product = Product::find($production_id); | |
$product->stock++; | |
$product->save(); | |
// ✔️ | |
Product::find($production_id)->increment('stock'); // +1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const segmenterEn = new Intl.Segmenter('en', { granularity: 'word' }); | |
const string1 = 'Split JavaScript Strings!'; | |
[...segmenterEn.segment(string1)] | |
// [ | |
// {segment: 'Split', index: 0, input: 'Split JavaScript Strings!', isWordLike: true} | |
// {segment: ' ', index: 5, input: 'Split JavaScript Strings!', isWordLike: false} | |
// {segment: 'JavaScript', index: 6, input: 'Split JavaScript Strings!', isWordLike: true} | |
// ... | |
// ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ❌ | |
$apiKey = env('API_KEY'); | |
// ✔️ | |
// config/api.php | |
'key' => env('API_KEY'), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function is_anagram($a, $b) { | |
return(count_chars($a, 1) == count_chars($b, 1)); | |
} | |
//Example: | |
$a = 'Silent'; | |
$b = 'Listen'; | |
echo is_anagram($a,$b); // output: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestCommand extends Command | |
{ | |
public function handle() | |
{ | |
$email = $this->anticipate('Find customer by email: ', [ | |
'Gopi@vigowebs.com', | |
'Vigo@example.com', | |
]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ❌ | |
if (! $collection->has($key)) { | |
$collection->put($key, $this->builtItem($data)); | |
} | |
return $collection->get($key); | |
// ✔️ | |
return $collection->getOrPut($key, fn () => $this->buildItem($data)); |