Skip to content

Instantly share code, notes, and snippets.

@scr2em
Created August 5, 2023 17:00
Show Gist options
  • Save scr2em/cc2faafcdd2e71c1d14a801eccc4fbf3 to your computer and use it in GitHub Desktop.
Save scr2em/cc2faafcdd2e71c1d14a801eccc4fbf3 to your computer and use it in GitHub Desktop.
for ITI summer training 2023
// The toFixed function should convert a number to a string, keeping only the specified number of decimal places
function toFixed(num, precision) {
// your code here
}
console.assert(toFixed(2.1234, 1) === "2.1", "toFixed test 1 failed");
console.assert(toFixed(3.5678, 2) === "3.57", "toFixed test 2 failed");
console.assert(toFixed(0.7890, 3) === "0.789", "toFixed test 3 failed");
// The charAt function should return the character at the specified index in a string
function charAt(str, index) {
// your code here
}
console.assert(charAt("course", 2) === "u", "charAt test 1 failed");
console.assert(charAt("openai", 0) === "o", "charAt test 2 failed");
console.assert(charAt("gpt-4", 4) === "4", "charAt test 3 failed");
// The concat function should concatenate two strings and return the result
function concat(str1, str2) {
// your code here
}
console.assert(concat("I love", " js") === "I love js", "concat test 1 failed");
console.assert(concat("Hello", " world") === "Hello world", "concat test 2 failed");
console.assert(concat("Open", "AI") === "OpenAI", "concat test 3 failed");
// The indexOf function should return the position of the first occurrence of a specified value in a string
function indexOf(str, char) {
// your code here
}
console.assert(indexOf("Youtube", "o") === 1, "indexOf test 1 failed");
console.assert(indexOf("javascript", "s") === 4, "indexOf test 2 failed");
console.assert(indexOf("OpenAI", "n") === 3, "indexOf test 3 failed");
// The slice function should extract a section of a string and returns a new string
function slice(str, start, end) {
// your code here
}
console.assert(slice("google", 1, 3) === "oog", "slice test 1 failed");
console.assert(slice("computer", 3, 6) === "put", "slice test 2 failed");
console.assert(slice("JavaScript", 4, 10) === "Script", "slice test 3 failed");
// The toUpperCase function should convert a string to uppercase letters
function toUpperCase(str) {
// your code here
}
console.assert(toUpperCase("hello") === "HELLO", "toUpperCase test 1 failed");
console.assert(toUpperCase("world") === "WORLD", "toUpperCase test 2 failed");
console.assert(toUpperCase("openai") === "OPENAI", "toUpperCase test 3 failed");
// The toLowerCase function should convert a string to lowercase letters
function toLowerCase(str) {
// your code here
}
console.assert(toLowerCase("WORLD") === "world", "toLowerCase test 1 failed");
console.assert(toLowerCase("HELLO") === "hello", "toLowerCase test 2 failed");
console.assert(toLowerCase("OPENAI") === "openai", "toLowerCase test 3 failed");
// The trim function should remove whitespace from both ends of a string
function trim(str) {
// your code here
}
console.assert(trim(" extra spaces ") === "extra spaces", "trim test 1 failed");
console.assert(trim(" openai ") === "openai", "trim test 2 failed");
console.assert(trim(" gpt-4 ") === "gpt-4", "trim test 3 failed");
// The push function should add a new item to an array and return the new array
function push(arr, item) {
// your code here
}
console.assert(JSON.stringify(push([1, 2], 3)) === JSON.stringify([1, 2, 3]), "push test 1 failed");
console.assert(JSON.stringify(push(['a', 'b'], 'c')) === JSON.stringify(['a', 'b', 'c']), "push test 2 failed");
console.assert(JSON.stringify(push([true, false], true)) === JSON.stringify([true, false, true]), "push test 3 failed");
// The pop function should remove the last item from an array and return the new array
function pop(arr) {
// your code here
}
console.assert(JSON.stringify(pop([1, 2, 3])) === JSON.stringify([1, 2]), "pop test 1 failed");
console.assert(JSON.stringify(pop(['a', 'b', 'c'])) === JSON.stringify(['a', 'b']), "pop test 2 failed");
console.assert(JSON.stringify(pop([true, false, true])) === JSON.stringify([true, false]), "pop test 3 failed");
// The shift function should remove the first item from an array and return the new array
function shift(arr) {
// your code here
}
console.assert(JSON.stringify(shift([1, 2, 3])) === JSON.stringify([2, 3]), "shift test 1 failed");
console.assert(JSON.stringify(shift(['a', 'b', 'c'])) === JSON.stringify(['b', 'c']), "shift test 2 failed");
console.assert(JSON.stringify(shift([true, false, true])) === JSON.stringify([false, true]), "shift test 3 failed");
// The unshift function should add a new item at the start of an array and return the new array
function unshift(arr, item) {
// your code here
}
console.assert(JSON.stringify(unshift([1, 2], 3)) === JSON.stringify([3, 1, 2]), "unshift test 1 failed");
console.assert(JSON.stringify(unshift(['a', 'b'], 'c')) === JSON.stringify(['c', 'a', 'b']), "unshift test 2 failed");
console.assert(JSON.stringify(unshift([true, false], true)) === JSON.stringify([true, true, false]), "unshift test 3 failed");
// The slice function should return a shallow copy of a portion of an array
function sliceArray(arr, start, end) {
// your code here
}
console.assert(JSON.stringify(sliceArray([1, 2, 3, 4], 1, 3)) === JSON.stringify([2, 3]), "sliceArray test 1 failed");
console.assert(JSON.stringify(sliceArray(['a', 'b', 'c', 'd'], 2, 4)) === JSON.stringify(['c', 'd']), "sliceArray test 2 failed");
console.assert(JSON.stringify(sliceArray([true, false, true, false], 0, 2)) === JSON.stringify([true, false]), "sliceArray test 3 failed");
// The map function should create a new array with the results of calling a provided function on every element in the array
function map(arr, func) {
// your code here
}
console.assert(JSON.stringify(map([1, 2, 3], x => x * 2)) === JSON.stringify([2, 4, 6]), "map test 1 failed");
console.assert(JSON.stringify(map(['a', 'b', 'c'], x => x.toUpperCase())) === JSON.stringify(['A', 'B', 'C']), "map test 2 failed");
console.assert(JSON.stringify(map([true, false, true], x => !x)) === JSON.stringify([false, true, false]), "map test 3 failed");
// The filter function should create a new array with all elements that pass the test implemented by the provided function
function filter(arr, func) {
// your code here
}
console.assert(JSON.stringify(filter([1, 2, 3, 4], x => x % 2 === 0)) === JSON.stringify([2, 4]), "filter test 1 failed");
console.assert(JSON.stringify(filter(['a', 'b', 'c', 'd'], x => x === 'b' || x === 'd')) === JSON.stringify(['b', 'd']), "filter test 2 failed");
console.assert(JSON.stringify(filter([true, false, true, false], x => x)) === JSON.stringify([true, true]), "filter test 3 failed");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment