Created
April 26, 2024 06:53
-
-
Save vtanathip/3e14a6afe34b10337c7712e0bea9509e to your computer and use it in GitHub Desktop.
Sample question for test fundamental knowledge of JS
This file contains hidden or 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
export {}; | |
function findClosestNumber(arr: number[], target: number): number { | |
// Sort the array in ascending order | |
arr.sort((a, b) => a - b); | |
let closest: number = arr[0]; | |
let minDifference: number = Math.abs(target - closest); | |
arr.forEach((num) => { | |
const difference: number = Math.abs(target - num); | |
if (difference < minDifference) { | |
minDifference = difference; | |
closest = num; | |
} | |
}); | |
return closest; | |
} | |
// Example usage: | |
let arr: number[] = [2, 5, 6, 7, 8, 8, 9]; | |
let target = 4; | |
let result: number = findClosestNumber(arr, target); | |
console.log(`Closest number to ${target} is ${result}`); |
This file contains hidden or 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
export {}; | |
function closestNumbers(arr: number[]): number[] { | |
// Sort the array | |
arr.sort((a, b) => a - b); | |
let minDiff = Number.MAX_SAFE_INTEGER; | |
let result: number[] = []; | |
// Calculate the minimum absolute difference | |
for (let i = 1; i < arr.length; i++) { | |
const diff = Math.abs(arr[i] - arr[i - 1]); | |
if (diff < minDiff) { | |
minDiff = diff; | |
result = [arr[i - 1], arr[i]]; | |
} else if (diff === minDiff) { | |
result.push(arr[i - 1], arr[i]); | |
} | |
} | |
return result; | |
} | |
let array = [-20, -3916237, -357920, -3620601, 7374819, -7330761, 30, 6246457, -6461594, 266854, -520, -470]; | |
let result = closestNumbers(array); | |
console.log(result.join(' ')); // Output: "-520 -470 -20 30" |
This file contains hidden or 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
export {} | |
// map | |
const numbers = [1, 2, 3, 4]; | |
const doubled = numbers.map((item,index,number) => item * 2); | |
console.log(doubled); // [2, 4, 6, 8] | |
// filter | |
const students = [ | |
{ name: 'Quincy', grade: 96 }, | |
{ name: 'Jason', grade: 84 }, | |
{ name: 'Alexis', grade: 100 }, | |
{ name: 'Sam', grade: 65 }, | |
{ name: 'Katie', grade: 90 } | |
]; | |
const studentGrades = students.filter(student => student.grade >= 90); | |
console.log('My object: ', studentGrades); // [ { name: 'Quincy', grade: 96 }, { name: 'Alexis', grade: 100 }, { name: 'Katie', grade: 90 } ] | |
// reduce | |
const numbers_reduce = [1, 2, 3, 4]; | |
const sum = numbers_reduce.reduce(function (result, item) { | |
return result + item; | |
}, 0); | |
console.log(sum); // 10 |
This file contains hidden or 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
export {}; | |
function cutTheSticks(arr: number[]): number[] { | |
const result: number[] = []; | |
// Sort the array in ascending order | |
arr.sort((a, b) => a - b); | |
// While there are sticks remaining | |
while (arr.length > 0) { | |
// Print the number of sticks remaining | |
result.push(arr.length); | |
// Find the length of the shortest stick | |
const shortest = arr[0]; | |
// Cut all sticks to the length of the shortest stick | |
arr = arr.map(stick => stick - shortest).filter(stick => stick > 0); | |
} | |
return result; | |
} | |
// Example usage: | |
const sticks = [5, 4, 4, 2, 2, 8]; | |
const result = cutTheSticks(sticks); | |
console.log(result); // Output: [6, 4, 2, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment