Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lab completed #74

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 63 additions & 9 deletions data/data.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,72 @@
const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0];

const words = [
"The",
"quick",
"brown",
"fox",
"jumps",
"over",
"the",
"lazy",
"dog",
'The',
'quick',
'brown',
'fox',
'jumps',
'over',
'the',
'lazy',
'dog',
];

const products = [
{
name: 'fairy lights',
price: 5.99,
description: 'festive holiday decoration',
},
{
name: 'banana',
price: 0.99,
description: 'full of potassium',
},
{
name: 'egg separator',
price: 3.99,
description: 'it separates yolks from whites',
},
{
name: 'flag',
price: 5.99,
description: 'catches the breeze',
},
{
name: 'quark',
price: 0.01,
description: 'Very small',
},
{
name: 'turtleneck',
price: 19.99,
description: 'available in black and slightly-darker black',
},
{
name: 'mitt (leather)',
price: 15,
description: 'regulation sized',
},
{
name: 'nothing',
price: 10,
description: "Hey, if you pay us, we won't ask any questions.",
},
{
name: 'violin',
price: 2000,
description: 'Talk about a JS fiddle...',
},
{
name: 'yoyo',
price: 1,
description: 'We had to pull some strings to get this one in.',
},
];

module.exports = {
nums,
words,
products,
};
111 changes: 84 additions & 27 deletions solution.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,118 @@
const { nums, words } = require("./data/data.js");
const { nums, words, products } = require('./data/data.js');

// Every
const isEveryNumGreaterThan2 = () => {
//
const isEveryNumGreaterThan2 = (nums) => {
return nums.every((num) => num >= 2);
};

const isEveryWordShorterThan7 = () => {
//
return words.every((word) => word.length < 7);
};

// Filter

const arrayLessThan5 = () => {
//
const arrayLessThan5 = (nums) => {
return nums.filter((num) => num < 5);
};

const arrayOddLengthWords = () => {
//
return words.filter((word) => word.length % 2 !== 0);
};

// Find

const firstValDivisibleBy4 = () => {
//
const firstValDivisibleBy4 = (nums) => {
return nums.find((num) => num % 4 === 0);
};

const firstWordLongerThan4Char = () => {
//
const firstWordLongerThan4Char = (words) => {
return words.find((word) => word.length > 4);
};

// Find Index

const firstNumIndexDivisibleBy3 = () => {
//
const firstNumIndexDivisibleBy3 = (nums) => {
return nums.findIndex((num) => num % 3 === 0);
};

const firstWordIndexLessThan2Char = () => {
//
const firstWordIndexLessThan2Char = (words) => {
return words.findIndex((word) => word.length < 2);
};

// For Each

const logValuesTimes3 = () => {
//
const logValuesTimes3 = (nums) => {
nums.forEach((num) => console.log(num * 3));
};

const logWordsWithExclamation = () => {
//
const logWordsWithExclamation = (words) => {
words.forEach((word) => console.log(`${word}!`));
};

// Map

const arrayValuesSquaredTimesIndex = () => {
//
const arrayValuesSquaredTimesIndex = (nums) => {
return nums.map((num, index) => num ** 2 * index);
};

const arrayWordsUpcased = () => {
//
const arrayWordsUpcased = (words) => {
return words.map((word) => word.toUpperCase());
};

// Some

const areSomeNumsDivisibleBy7 = () => {
//
const areSomeNumsDivisibleBy7 = (nums) => {
return nums.some((ele) => ele % 7 === 0);
};

const doSomeWordsHaveAnA = () => {
//
const doSomeWordsHaveAnA = (words) => {
return words.some((ele) => ele.toLowerCase().includes('a'));
};

const addAllNumbers = (nums) => {
return nums.reduce((acc, cur) => acc + cur, 0);
};

const concatAllWords = (words) => {
return words.reduce((acc, cur) => acc + cur, '');
};

const sortNumsWithoutArguments = (nums) => {
return nums.sort();
};

const sortWordsWithoutArgs = (words) => {
return words.sort();
};

const sortNumsAsc = (nums) => {
return nums.sort((a, b) => a - b);
};
const sortNumsDesc = (nums) => {
return nums.sort((a, b) => b - a);
};

const sortWordsAsc = (words) => {
return words.sort((a, b) => (a > b ? 1 : a < b ? -1 : 0));
};
const sortWordsDesc = (words) => {
return words.sort((a, b) => (b > a ? 1 : b < a ? -1 : 0));
};

const letterUsedOnce = (words) => {
const str = [...'abcdefghijklmnopqrstuvwxyz'];
const lower = words.join(' ').toLowerCase();
returnstr.every((ele) => lower.includes(ele));
};

const filterProductPrice = (products) => {
return products.filter((product) => product.price < 10);
};

const filterProductName = (products) => {
return products.sort((a, b) =>
a.name < b.name ? -1 : a.name > b.name ? 1 : 0
);
};

module.exports = {
Expand All @@ -84,4 +130,15 @@ module.exports = {
arrayWordsUpcased,
areSomeNumsDivisibleBy7,
doSomeWordsHaveAnA,
addAllNumbers,
concatAllWords,
sortWordsWithoutArgs,
sortNumsWithoutArguments,
sortWordsAsc,
sortWordsDesc,
sortNumsAsc,
sortNumsDesc,
letterUsedOnce,
filterProductPrice,
filterProductName,
};