Skip to content

Instantly share code, notes, and snippets.

@maheshgiri
Created March 14, 2022 16:33
Show Gist options
  • Save maheshgiri/715e148c1b137f797194a0680795a184 to your computer and use it in GitHub Desktop.
Save maheshgiri/715e148c1b137f797194a0680795a184 to your computer and use it in GitHub Desktop.
JavaScript Arrays
//Write the function camelize(str) that changes dash-separated words like “my-short-string” into camel-cased “myShortString”.
//That is: removes all dashes, each word after dash becomes uppercased.
function toCamelCase(str){
return str.split("-").map((item,index)=> index===0 ? item : item[0).toUpperCase()+item.slice(1)).join();
}
//Write a function filterRange(arr, a, b) that gets an array arr, looks for elements with values higher or equal to a and lower or equal to b and return a result as an array.
const result=(
function filterRange(arr,a,b){
const result= arr.filter((item,index)=> item>=a &&item <= b)
return result
}
)([5,63,2,1], 3, 62);
console.log(result)
//Write a function filterRangeInPlace(arr, a, b) that gets an array arr and removes from it all values except those that are between a and b. The test is: a ≤ arr[i] ≤ b.
//The function should only modify the array. It should not return anything.
const result=(
function filterRange(arr,a,b){
arr.forEach(item=>{
if(!(item>=a && item<=b)){
arr.splice(arr.indexOf(item),1)
}
})
return arr
}
)([5, 3, 8, 1], 1, 4);
console.log(result)
//
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment