Skip to content

Instantly share code, notes, and snippets.

function myConcat(separator) {
var result = ""; // initialize list
var i;
// iterate through arguments
for (i = 1; i < arguments.length; i++) {
result += arguments[i] + separator;
}
return result;
}
// Flatten an Array of Arrays without using .flat()
function flatten(array) {
return array.reduce((acc, item) => {
if (Array.isArray(item)) {
acc = acc.concat(flatten(item));
} else {
acc.push(item);
}
return acc;
document.getElementById("app").innerHTML = `
<h1>Hello Vanilla!</h1>
<div>
We use the same configuration as Parcel to bundle this sandbox, you can find more
info about Parcel
<a href="https://parceljs.org" target="_blank" rel="noopener noreferrer">here</a>.
</div>
`;
var marks = { physics: 98, maths: 95, chemistry: 91 };
// 1. ID base case
// 2. ID recursive case
// 3. Return where appropriate
// 4. Write procedures that bring you closer to base case
// Simple factorial example with Recursion
const factorial = n => {
return n < 2 ? 1 : n * factorial(n - 1);
};
// Use .bind() when you want that function to later be called
// with a certain context, useful in events.
// Use .call() or .apply() when you want to invoke the
// function immediately, with modification of the context.
// implement Function.prototype.bind()
/*
const foo = function() {
console.log(this.bar);
/*
Hi there! Thanks for taking on this code test. The requirements are listed below:
1. Create a "Foods" class or constructor that will take two arguements: a root element and a data object (foodData).
2. Render all of the items in the data object into the DOM with the root element as the parent
3. If the user clicks a food item, it should be removed from the list
Rules:
- Only vanilla JS
- Feel free to use Google, Bing, DuckDuckGo to look things up
// Reduce can be used to sum all products
const products = [{ price: 1 }, { price: 2 }, { price: 3 }, { price: 4 }];
function sumAllPrices(products) {
return products.reduce((sum, product) => sum + product.price, 0);
}
sumAllPrices(products); // 425
const a = [1, 2, 3];
const b = [4, 5, 6];
const c = [1, 2, 3];
function arrayEquals(a, b) {
return (
Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((val, index) => val === b[index])