Skip to content

Instantly share code, notes, and snippets.

const _ = {
clamp(number, lower, upper) {
const lowerClampedValue = Math.max(number, lower);
const clampedValue = Math.min(lowerClampedValue, upper);
return clampedValue;
},
inRange(number, start, end) {
if (end === undefined) { end = start, start = 0 }
if (start > end) { [start, end] = [end, start] }
type Restaurant = {
name: string;
priceBracket: PriceBracket;
deliveryTimeMinutes: number;
openHour: number;
closeHour: number;
distance: number;
};
type Order = {
// Returns a random DNA base
const returnRandBase = (takenBase) => {
const dnaBases = ["A", "T", "C", "G"];
if (takenBase) {
dnaBases.splice(dnaBases.indexOf(takenBase),1)
}
return dnaBases[Math.floor(Math.random() * dnaBases.length)];
};
// Returns a random single strand of DNA containing 15 bases
// Exercise 1
const indexOf = (arr, item) => {
let output
if (!arr.includes(item)) { output = -1 }
for (const i in arr) {
(arr[i] === item) ? output = i : false
}
return output
}
console.log(indexOf([1, 2, 3, 4], 3))
let person = {
first: "Alberto",
last: "Montalesi"
}
const {first , last} = person
console.log(first,last);
const { facebook } = {
...person,
@Merce0897
Merce0897 / index.js
Last active September 24, 2022 03:40
// Test 1
const subLength = (string,occurencies) => {
let indexArray = []
for (let i = 0; i < string.length; i++) {
(string[i] === occurencies) ? indexArray.push(i) : false
}
return (indexArray.length > 2 || indexArray.length < 2) ? 0 : indexArray[1] - indexArray[0] + 1
}
// Test 2
const tinhDiem = (mon1, mon2, mon3, khuVuc, doiTuong, diemChuan) => {
if (mon1 ===0 || mon2 === 0 || mon3 === 0) {
return 'Rớt'
}
let tongDiem = mon1 + mon2 + mon3
if (khuVuc === 'A') {
tongDiem += 2
} else if (khuVuc === 'B') {
tongDiem += 1
} else if (khuVuc === 'C') {
// Challenge 1
const fName = "Vinh"
const lName = "Truong"
const age = 25
const profession = "FE Dev"
const salary = 10
console.log(`My name is ${fName} ${lName}, i'm ${age} years old. I work as ${profession} and make ${salary}$`);
const peopleNameArray = ["William", "Collin", "Stephen"];
const showPeopleName = function (name) {
console.log("Hello " + name);
}
peopleNameArray.forEach(showPeopleName)
peopleNameArray.forEach(showPeopleName)
// Sign of the Product of an Array.
var arraySign = function(nums) {
let product = 1
for (let i = 0; i < nums.length; i++) {
product *= nums[i]
}
if (product > 0) {
return 1
} else if (product < 0) {
return -1