Skip to content

Instantly share code, notes, and snippets.

@umair-khokhar
Created September 19, 2019 17:42
Show Gist options
  • Save umair-khokhar/9e822517e7bf371333accb35668b7d1c to your computer and use it in GitHub Desktop.
Save umair-khokhar/9e822517e7bf371333accb35668b7d1c to your computer and use it in GitHub Desktop.
1. Write a custom method to replace the string "The quick brown fox jumps
over the lazy dog" with the string "The1 quick2 brown3 fox4 jumps5
over6 the7 lazy8 dog9"
const transformStr = (str) => {
console.log(this);
let strArr = str.split(' ');
let numArr = Array(strArr.length).fill().map((v, i) => i = i + 1);
return strArr.map((v, i) => v + numArr[i]).join(' ')
}
let str = '"The quick brown fox jumps over the lazy dog';
console.log(transformStr(str));
2. Using a single line of code, reverse the order of words in the string below:
var dwarves = "bashful doc dopey grumpy happy sleepy sneezy";
const reverseOrder = (str) => {
return str.split(' ').reverse().join(' ');
};
var dwarves = "bashful doc dopey grumpy happy sleepy sneezy";
console.log(reverseOrder(dwarves));
3. Write a function that takes a number (from 1 to 12) and return its corresponding month
name as a string.
const getMonth = (num) => {
const months = ["January","February","March","April","May","June","July",
"August","September","October","November","December"];
return months[num - 1];
}
console.log(getMonth(1));
4. Write a regular expression that matches any string containing at least one digit.
const checkIfContainDigit = (str) => {
const rExp = /\d/
return rExp.test(str);
}
console.log(checkIfContainDigit('I am 2'));
5. Write a function that returns true if two arrays are identical, and false otherwise.
const checkIfIdentical = (arr1, arr2) => {
if(arr1.join('') == arr2.join('')) {
return true;
}
return false;
}
console.log(checkIfIdentical(['a', 'b', 'c'], ['a', 'b', 'c']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment