scrimba
Helen Chong's JavaScriptmas 2023 Solutions
Helen Chong's JavaScriptmas 2023 Day 12 solution
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Helen Chong's JavaScriptmas 2023 Day 12 solution
AboutCommentsNotes
Helen Chong's JavaScriptmas 2023 Day 12 solution
Expand for more info
index.js
run
preview
console
const xmasGifts = ['guitar 🎸', 'skates ⛸️', 'bear 🧸', 'magnet 🧲', 'laptop 💻', 'games console 🎮 ', 'jewellery 💍', 'kite 🪁']

/**
* Challenge:
* 1. Sort the array twice. Alphabetically and reverse alphabetically.
**/

const sortedAZ = xmasGifts.sort() /* write code here */
console.log('A-Z: ', sortedAZ)
//["bear 🧸", "games console 🎮 ", "guitar 🎸", "jewellery 💍", "kite 🪁", "laptop 💻", "scarf 🧣", "skates ⛸️"]

// const sortedZA = xmasGifts.sort().reverse() /* write code here */
const sortedZA = reverseArray(xmasGifts.sort()) /* write code here */
console.log('Z-A: ', sortedZA)
//["skates ⛸️", "scarf 🧣", "laptop 💻", "kite 🪁", "jewellery 💍", "guitar 🎸", "games console 🎮 ", "bear 🧸"]

function reverseArray(arr) {
for(let i = 0; i < arr.length / 2; i++) {
let num = (arr.length - i) - 1
let tmp = arr[i]
arr[i] = arr[num]
arr[num] = tmp
}
return arr
}
Console
"A-Z: "
,
[
"bear 🧸"
,
"games console 🎮 "
,
"guitar 🎸"
,
"jewellery 💍"
,
"kite 🪁"
,
"laptop 💻"
,
"magnet 🧲"
,
"skates ⛸️"
]
,
"Z-A: "
,
[
"skates ⛸️"
,
"magnet 🧲"
,
"laptop 💻"
,
"kite 🪁"
,
"jewellery 💍"
,
"guitar 🎸"
,
"games console 🎮 "
,
"bear 🧸"
]
,
/index.html
LIVE