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 letter = 'abcdefghijklmnopqrstuvwxyz'; | |
const allCharacters = `${letter}1234567890123456789123456789$&@*£€¥%${letter.toUpperCase()}`; | |
const allCharactersInArray = allCharacters.split(''); | |
function randomise() { | |
const randomCharacter = | |
allCharactersInArray[Math.floor(Math.random() * allCharactersInArray.length)]; | |
return randomCharacter; | |
} |
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
// i still feel like there is a better way to do this, but this is what i came up with. If you know a better way please share | |
const oldObject = { name: 'John Doe', nationality: 'South Africa' }; | |
const newObject = { name: 'John Doe Seth' }; | |
function changeDataInObjects(newData, oldData) { | |
for (let i in oldData) { | |
for (let j in newData) { | |
if (i === j) { // i and j represents the property names of oldData and newData respectively | |
oldData[i] = newData[j]; | |
} else { |
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
// spread operators allows any iterable to be expanded | |
// it can serve as a means of adding arrays (concatenation) | |
const arr1 = [1, 3, 4] | |
const arr2 = [10, 13, 5] | |
const bothArr = [...arr1, ...arr2] | |
// you can use this to update an array with | |
// another array without leading to a multidimensional array | |
let arr1 = [1, 3, 4] | |
const arr2 = [10, 13, 5] |
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 checkForPalindrome(phrase) { | |
const phraseInArr = phrase.split(''); | |
const noWhiteSpace = phraseInArr.filter((phrase) => { | |
return phrase !== ' '; // you can use regex to ignore characters | |
}); | |
const toUseLength = Math.floor(noWhiteSpace.length / 2); | |
const firstSet = noWhiteSpace.slice(0, toUseLength).join('').toLowerCase(); |
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 reverseNumber(givenNum) { | |
const numInArr = givenNum.toString().split(''); | |
numInArr.reverse(); | |
return +numInArr.join(''); | |
} |
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
// reversing a number | |
let givenNum = 4552; | |
let numInRev = 0; | |
while (givenNum > 0) { | |
const lastDigit = Math.floor(givenNum % 10); | |
numInRev = numInRev * 10 + lastDigit; | |
givenNum = Math.floor(givenNum / 10); | |
} |
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
# TODO: python delete file and folders | |
import os | |
# create file | |
html = open('demo.html', 'x') | |
html.close() | |
# write to new file | |
html = open('demo.html', 'w') | |
html.write('<html>\n<body>\n<p>\nHello World</p>\n</body>\n</html>') |
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
// convert from array to list with | |
function arrayToList(arr) { | |
// list format: value 1, rest => value 2, rest => value 3, rest => null | |
let list; | |
for (let i = arr.length; i >= 0; i--) { | |
list = { | |
value: arr[i], | |
rest: i >= arr.length - 1 ? null : list, | |
}; | |
} |
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
// reverse an array that modifies the array | |
function reverseArrayInPlace(arr) { | |
let length = arr.length; | |
for (let i = length - 1; i >= 0; i--) { | |
arr.push(arr[i]); | |
} | |
arr.splice(0, length); | |
return arr; | |
} |
OlderNewer