Learn to Cook Up JavaScript Spells: So Easy Even Grandma Can Do It

codetobug

codeToBug

Posted on June 7, 2023

Learn to Cook Up JavaScript Spells: So Easy Even Grandma Can Do It

Crack your knuckles, aspiring coders! This is your jargon-free, absurdly easy guide to basic JavaScript that's so friendly, even your grandma could ace it (We love you, grandma!).

1. Summoning the Spirit of Fibonacci: Making an Array from a Range of Numbers

Here, let’s conjure an array from a set range of numbers because, why not? You know, just like making cookies but without the calories.

let arrayFromRange = (min, max) => Array.from({length: max - min + 1}, (_, i) => min + i);
Enter fullscreen mode Exit fullscreen mode

2. An Existential Dilemma: Odd or Even?

Next, a conundrum that has baffled philosophers for eons - is the number odd or even? Such profound questions, such riveting suspense.

let isEven = num => num % 2 === 0;
Enter fullscreen mode Exit fullscreen mode

3. Word Surgery: Extracting a Bit of a Text String

Time for a little surgery. Scapel...er, I mean, substring, please! Let’s extract that stubborn bit of text.

let extractSubString = (str, start, end) => str.substring(start, end);
Enter fullscreen mode Exit fullscreen mode

4. Identity Switch Operation: Replacing a Text String

Next, we perform the mystical art of identity change. Who knew text strings could be so flexible?

let replaceString = (str, oldStr, newStr) => str.replace(oldStr, newStr);
Enter fullscreen mode Exit fullscreen mode

5. The Beginning and End of the World: Checking if a Text String Starts or Ends with Certain Words

Does it start? Does it end? Just like reading an intriguing novel, but with less drama and more determinism.

let startsOrEndsWith = (str, checkStr) => str.startsWith(checkStr) || str.endsWith(checkStr);
Enter fullscreen mode Exit fullscreen mode

6. A Deep Clean: Removing All Empty Spaces from a Text String

Now let’s make that messy string squeaky clean. Sorry, spaces, you’ve overstayed your welcome!

let removeSpaces = str => str.replace(/\s+/g, '');
Enter fullscreen mode Exit fullscreen mode

7. Superhero Strength: Getting the Power of a Number

Get ready to flex your mathematical muscles. Time to find the power of a number.

let powerOfNumber = (num, power) => Math.pow(num, power);
Enter fullscreen mode Exit fullscreen mode

8. Crystal Ball: Checking if an Object has a Specific Property

Here we peer into the murky depths of an object, divining its properties. It’s less spooky than it sounds, promise!

let hasProperty = (obj, prop) => obj.hasOwnProperty(prop);
Enter fullscreen mode Exit fullscreen mode

9. A Mysterious Double: Creating a Copy of an Object

Don't want to mess with the original? Make a doppelgänger. It's like having a twin, but with less sibling rivalry.

let copyObject = obj => Object.assign({}, obj);
Enter fullscreen mode Exit fullscreen mode

10. Magical Vanishing Act: Removing a Property from an Object

Now, for the pièce de résistance, we're going to make a property disappear! Just like magic, but less flashy.

let removeProperty = (obj, prop) => { delete obj[prop]; return obj; };
Enter fullscreen mode Exit fullscreen mode

11. A World Without Clones: Removing Duplicate Items from an Array

Duplicates are so passé. Let’s spring clean that array!

let removeDuplicates = array => [...new Set(array)];
Enter fullscreen mode Exit fullscreen mode

12. Two-Headed Monster: Merging Two Arrays or Objects

Lastly, the beast of all beasts - merging arrays or objects. Don't worry, no actual monsters were harmed.

let mergeItems = (item1, item2) => Array.isArray(item1) ? [...item1, ...item2] : { ...item1, ...item2 };
Enter fullscreen mode Exit fullscreen mode

And voilà! You’ve now dipped your toes into the enchanting world of JavaScript. Remember, practice makes perfect and if your grandma can do it, so can you. Get coding!

💖 💪 🙅 🚩
codetobug
codeToBug

Posted on June 7, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related