Skip to content

Instantly share code, notes, and snippets.

@fadjriaf
Last active August 5, 2022 03:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fadjriaf/59a9ea565bff047f16c558ffd637d6cc to your computer and use it in GitHub Desktop.
Save fadjriaf/59a9ea565bff047f16c558ffd637d6cc to your computer and use it in GitHub Desktop.
Code Challenges: Intermediate JavaScript

Codecademy Web Development Path

https://www.codecademy.com/learn/paths/web-development

JavaScript: Arrays, Loops, and Objects

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects

Code Challenges: Intermediate JavaScript

https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/web-dev-intermediate-javascript-practice/lessons/intermediate-javascript-coding-challenge

  1. reverseArray()
  2. greetAliens()
  3. convertToBaby()
  4. Fix The Broken Code!
  5. declineEverything() and acceptEverything()
  6. squareNums()
  7. shoutGreetings()
  8. sortYears()
  9. justCoolStuff()
  10. isTheDinnerVegan()
  11. sortSpeciesByTeeth()
  12. findMyKeys()
  13. dogFactory()
// Write your code here:
const convertToBaby = arr => {
const babyArray = [];
for (let i = 0; i < arr.length; i++) {
babyArray.push('baby ' + arr[i]);
}
return babyArray
}
// When you're ready to test your code, uncomment the below and run:
const animals = ['panda', 'turtle', 'giraffe', 'hippo', 'sloth', 'human'];
console.log(convertToBaby(animals))
const veggies = ['broccoli', 'spinach', 'cauliflower', 'broccoflower'];
const politelyDecline = (veg) => {
console.log('No ' + veg + ' please. I will have pizza with extra cheese.');
}
// Write your code here:
const declineEverything = arr => {
arr.forEach(politelyDecline);
}
const acceptEverything = arr => {
arr.forEach(e => {
console.log(`Ok, I guess I will eat some ${e}.`)
})
}
const dogFactory = (name, breed, weight) => {
return {
_name: name,
_breed: breed,
_weight: weight,
get name() {
return this._name;
},
set name(newName) {
this._name = newName;
},
get breed() {
return this._breed;
},
set breed(newBreed) {
this._breed = newBreed;
},
get weight() {
return this._weight;
},
set weight(newWeight) {
this._weight = newWeight;
},
bark() {
return 'ruff! ruff!'
},
eatTooManyTreats() {
this._weight++
}
}
}
// Write your code here:
const findMyKeys = arr => arr.findIndex(item => item === 'keys');
// Feel free to comment out the code below to test your function
const randomStuff = ['credit card', 'screwdriver', 'receipt', 'gum', 'keys', 'used gum', 'plastic spoon'];
console.log(findMyKeys(randomStuff))
// Should print 4
const numbers = [5, 3, 9, 30];
const smallestPowerOfTwo = arr => {
let results = [];
// The 'outer' for loop - loops through each element in the array
for (let i = 0; i < arr.length; i++) {
number = arr[i];
// The 'inner' while loop - searches for smallest power of 2 greater than the given number
let j = 1;
while (j < number) {
j = j * 2;
}
results.push(j);
}
return results
}
console.log(smallestPowerOfTwo(numbers))
// Should print the returned array [ 8, 4, 16, 32 ] instead prints the returned array [8]
// Write your code here:
const greetAliens = arr => {
for (let i = 0; i < arr.length; i++) {
console.log('Oh powerful ' + arr[i] + ', we humans offer our unconditional surrender!');
}
}
// When you're ready to test your code, uncomment the below and run:
const aliens = ["Blorgous", "Glamyx", "Wegord", "SpaceKing"];
greetAliens(aliens);
// Write your code here:
const isTheDinnerVegan = arr => arr.every(food => food.source === 'plant');
// Feel free to comment out the code below to test your function
const dinner = [
{
name: 'hamburger',
source: 'meat'
}, {
name: 'cheese',
source: 'dairy'
}, {
name: 'ketchup',
source:'plant'
}, {
name: 'bun',
source: 'plant'
}, {
name: 'dessert twinkies',
source:'unknown'
}
];
console.log(isTheDinnerVegan(dinner))
// Should print false
// Write your code here:
const justCoolStuff = (firstArray, secondArray) => firstArray.filter(item => secondArray.includes(item));
// Feel free to uncomment the code below to test your function
const coolStuff = ['gameboys', 'skateboards', 'backwards hats', 'fruit-by-the-foot', 'pogs', 'my room', 'temporary tattoos'];
const myStuff = [ 'rules', 'fruit-by-the-foot', 'wedgies', 'sweaters', 'skateboards', 'family-night', 'my room', 'braces', 'the information superhighway'];
console.log(justCoolStuff(myStuff, coolStuff))
// Should print [ 'fruit-by-the-foot', 'skateboards', 'my room' ]
// Write your code here:
const reverseArray = arr => {
let reversed = [];
for (let i = 0; i < arr.length; i++) {
reversed.unshift(arr[i]);
}
return reversed;
}
// When you're ready to test your code, uncomment the below and run:
const sentence = ['sense.','make', 'all', 'will', 'This'];
console.log(reverseArray(sentence))
// Should print ['This', 'will', 'all', 'make', 'sense.'];
// Write your code here:
const shoutGreetings = arr => arr.map(word => word.toUpperCase() + '!');
// Feel free to uncomment out the code below to test your function!
const greetings = ['hello', 'hi', 'heya', 'oi', 'hey', 'yo'];
console.log(shoutGreetings(greetings))
// Should print [ 'HELLO!', 'HI!', 'HEYA!', 'OI!', 'HEY!', 'YO!' ]
const speciesArray = [
{
speciesName:'shark', numTeeth:50
}, {
speciesName:'dog', numTeeth:42
}, {
speciesName:'alligator', numTeeth:80
}, {
speciesName:'human', numTeeth:32
}
];
// Write your code here:
const sortSpeciesByTeeth = arr => arr.sort((speObj1, speObj2) => speObj1.numTeeth > speObj2.numTeeth)
// Feel free to comment out the code below when you're ready to test your function!
console.log(sortSpeciesByTeeth(speciesArray))
// Should print [ { speciesName: 'human', numTeeth: 32 },
// { speciesName: 'dog', numTeeth: 42 },
// { speciesName: 'shark', numTeeth: 50 },
// { speciesName: 'alligator', numTeeth: 80 } ]
// Write your code here:
const sortYears = arr => arr.sort((x, y) => y - x);
// Feel free to uncomment the below code to test your function:
const years = [1970, 1999, 1951, 1982, 1963, 2011, 2018, 1922]
console.log(sortYears(years))
// Should print [ 2018, 2011, 1999, 1982, 1970, 1963, 1951, 1922 ]
const numbers = [2, 7, 9, 171, 52, 33, 14]
const toSquare = num => num * num
// Write your code here:
const squareNums = numbers => numbers.map(toSquare);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment