Searching Two Objects at Once [Edabit]

Edabit

Oh no! Your classmate slept in this morning and totally forgot to write an essay for his homework! He sent you a text asking if the class accepted late work, but he was in such a rush, he only sent the teacher’s name.

It’s your job to create a function that will search multiple objects to find the class your friend was talking about, and if they accept late work.

With the limited amount of classes your friend is taking, you might find it easier to just create a switch statement for every teacher, or search each object (class) individually. But not only does that take the fun out of the challenge, but your friend is also counting on you to find out this information fast; those overcomplicated methods just won’t do. Try to search all class objects at one time to maximize speed!

Example

acceptsLateWork("Mr. Citrano") ➞ false

Notes

  • The class objects are provided to you in the Code tab- don’t change them.
  • The teacher given will always be valid.
  • Each class has an “Advanced” and “Standard” curriculum, both with a different teacher. Make sure to consider them!

Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// english class object
const english = {
  advanced: {
    // advanced curriculum
    teacher: "Ms. Abrimian",
    acceptsLateWork: false,
  },
  standard: {
    // standard curriculum
    teacher: "Mr. Sheehan",
    acceptsLateWork: true,
  },
};

// math class object
const math = {
  advanced: {
    teacher: "Mr. Citrano",
    acceptsLateWork: false,
  },
  standard: {
    teacher: "Ms. Marinelli",
    acceptsLateWork: false,
  },
};

function acceptsLateWork(teacher) {
  let arr = [...Object.values(math), ...Object.values(english)];
  for (let obj of arr) {
    if (obj.teacher === teacher) {
      return obj.acceptsLateWork;
    }
  }
}

Check if All Values Are True [Edabit]

Edabit

Create a function that returns true if all parameters are truthy, and false otherwise.

Examples

allTruthy(true, true, true) ➞ true

allTruthy(true, false, true) ➞ false

allTruthy(5, 4, 3, 2, 1, 0) ➞ false

Notes

  • Falsy values include false0"" (empty string), nullundefined, and NaN; everything else is truthy.
  • You will always be supplied with at least one parameter.

Solution:

1
2
3
4
5
6
function allTruthy(...args) {
  for (let value of args) {
    if (!value) return false;
  }
  return true;
}

Expensive Words [Edabit]

Edabit

Each letter in a sentence is worth its position in the alphabet (i.e. a = 1, b = 2, c = 3, etc…). However, if a word is all in UPPERCASE, the value of that word is doubled.

Create a function which returns the value of a sentence.

getSentenceValue("abc ABC Abc") ➞ 24
// a = 1, b = 2, c = 3

// abc = 1 + 2 + 3 = 6
// ABC = (1+2+3) * 2 = 12 (ALL letters are in uppercase)
// Abc = 1 + 2 + 3 = 6 (NOT ALL letters are in uppercase)

// 6 + 12 + 6 = 24

Examples

getSentenceValue("HELLO world") ➞ 176

getSentenceValue("Edabit is LEGENDARY") ➞ 251

getSentenceValue("Her seaside sea-shelling business is really booming!") ➞ 488

Notes

  • Ignore spaces and punctuation.
  • Remember that the value of a word isn’t doubled unless all the letters in it are uppercase.

Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
function getSentenceValue(str) {
  str = str.replace(/[^a-zA-Z ]/g, "");
  let alpha = " abcdefghijklmnopqrstuvwxyz";
  let sol = 0;
  for (let word of str.split(" ")) {
    let temp = 0;
    for (let char of [...word]) {
      temp += alpha.indexOf(char.toLowerCase());
    }
    if (word.toUpperCase() === word) {
      sol += 2 * temp;
    } else {
      sol += temp;
    }
  }
  return sol;
}

Rearrange the Sentence [Edabit]

Edabit

Given a sentence with numbers representing a word’s location embedded within each word, return the sorted sentence.

Examples

rearrange("is2 Thi1s T4est 3a") ➞ "This is a Test"

rearrange("4of Fo1r pe6ople g3ood th5e the2") ➞ "For the good of the people"

rearrange(" ") ➞ ""

Notes

Only the integers 1-9 will be used.

Solution:

1
2
3
4
5
6
7
8
9
function rearrange(sentence) {
  let arr = sentence.trim().split(" ");
  arr.sort((a, b) => {
    return (
      parseInt(a.replace(/[^0-9]/g, "")) - parseInt(b.replace(/[^0-9]/g, ""))
    );
  });
  return arr.map((a) => a.replace(/[0-9]/g, "")).join(" ");
}

Extracting Words with “-ing” Inflection [Edabit]

Edabit

Write a function that takes a string as an argument and returns a list of all the words inflected by “-ing”. Your function should also exclude all the mono-syllabic words ending in “-ing” (e.g. bing, sing, sling, …). Although these words end in “-ing”, the “-ing” is not an inflection affix.

Examples

ingExtractor("coming bringing Letting sing") ➞ ["coming", "bringing", "Letting"]

ingExtractor("going Ping, king sHrink dOing") ➞ ["going",, "dOing"]

ingExtractor("zing went ring, ding wing SINk") ➞ []

Notes

  • Mono-syllabic means a word containing just one syllable.
  • It’s probably best to use RegEx for this challenge.

Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function ingExtractor(string) {
  return string
    .replace(/[^a-zA-Z ]/g, "")
    .split(" ")
    .filter((word) => {
      let temp = word.toLowerCase().replace("ing", "");
      if (temp.length === temp.replace(/[aeiou]/g, "").length) {
        return false;
      }
      if (word.toLowerCase().includes("ing")) {
        return true;
      }
    });
}

Sort Names According to the Length of Their Last Names [Edabit]

Edabit

Create a function that takes an array of names in the format “First Name Last Name” (e.g. “John Doe”), and returns an array of these names sorted by the length of their last names. If the length of multiple last names are the same, then proceed to sort alphabetically by last name.

Examples

lastNameLensort([
  "Jennifer Figueroa",
  "Heather Mcgee",
  "Amanda Schwartz",
  "Nicole Yoder",
  "Melissa Hoffman"
]) ➞ ["Heather Mcgee", "Nicole Yoder", "Melissa Hoffman", "Jennifer Figueroa", "Amanda Schwartz"]

Notes

If last names are of the same length, sort alphabetically by last name.

Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function lastNameLensort(names) {
  return names.sort((a, b) => {
    let first = a.split(" ")[1];
    let second = b.split(" ")[1];
    if (first.length == second.length) {
      return first.localeCompare(second);
    }
    return first.length - second.length;
  });
}

Record Temperatures [Edabit]

Edabit

You are given two arrays that each contain data that represents the min and max weather temperatures for each day of the week.

The records array contains the all-time record low/high temperatures for that day of the week.

[[record low, record high], ...]

The current week array contains the daily low/high temperatures for each day of the current week.

[[daily low, daily high], ...]

A daily high temperature is considered a new record high if it is higher than the record high for that day of the week. A daily low temperature is considered a new record low if it is lower than the record low for that day of the week.

Compare the daily low/high temperatures of the current week to the record lows/highs and return an array with the updated record temperatures.

  • There may be multiple record temperatures in a week.
  • If there are no broken records return the original records array.

Example

//             sun       mon      tues       wed      thur      fri       sat
recordTemps([[34, 82], [24, 82], [20, 89],  [5, 88],  [9, 88], [26, 89], [27, 83]],
            [[44, 72], [19, 70], [40, 69], [39, 68], [33, 64], [36, 70], [38, 69]])

➞           [[34, 82], [19, 82], [20, 89], [5, 88], [9, 88], [26, 89], [27, 83]]

The previous record low for Monday was 24. The current week’s low for Monday was 19. So 19 replaces 24 as Monday’s new record low.

Notes

  • There won’t be a record high and record low set on the same day.
  • Index 0 will always be the low and index 1 will always be the high [low, high].
  • For reference these temps are °F but you do not need to convert any temperatures.

Solution:

1
2
3
4
5
6
7
8
function recordTemps(records, currentWeek) {
  return records.map((record, i) => {
    return [
      Math.min(record[0], currentWeek[i][0]),
      Math.max(record[1], currentWeek[i][1]),
    ];
  });
}

Flick Switch [Edabit]

Edabit

Create a function that always returns true for every item in a given array. However, if an element is the word "flick", switch to always returning the opposite boolean value.

Examples

flickSwitch(["edabit", "flick", "eda", "bit"]) ➞ [true, false, false, false]

flickSwitch(["flick", 11037, 3.14, 53]) ➞ [false, false, false, false]

flickSwitch([false, false, "flick", "sheep", "flick"]) ➞ [true, true, false, false, true]

Notes

  • "flick" will always be given in lowercase.
  • An array may contain multiple flicks.
  • Switch the boolean value on the same element as the flick itself.

Solution:

1
2
3
4
5
6
7
8
9
function flickSwitch(arr) {
  let on = true;
  return arr.map((item) => {
    if (item === "flick") {
      on = !on;
    }
    return on;
  });
}

What’s the Missing Letter? [Edabit]

Edabit

Given a string of letters in the English alphabet, return the letter that’s missing from the string. The missing letter will make the string be in alphabetical order (from A to Z).

If there are no missing letters in the string, return "No Missing Letter".

Examples

missingLetter("abdefg") ➞ "c"

missingLetter("mnopqs") ➞ "r"

missingLetter("tuvxyz") ➞ "w"

missingLetter("ghijklmno") ➞ "No Missing Letter"

Notes

The given string will never have more than one missing letter.

Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function missingLetter(str) {
  let char = str[0].charCodeAt();
  for (let ch of [...str]) {
    if (String.fromCharCode(char) !== ch) {
      return String.fromCharCode(char);
    }
    char++;
  }
  return "No Missing Letter";
}

Reversing a Binary String [Edabit]

Edabit

Write a function that takes an integer n, reverses the binary representation of that integer, and returns the new integer from the reversed binary.

Examples

reversedBinaryInteger(10) ➞ 5
// 10 = 1010 -> 0101 = 5

reversedBinaryInteger(12) ➞ 3
// 12 = 1100 -> 0011 = 3

reversedBinaryInteger(25) ➞ 19
// 25 = 11001 -> 10011 = 19

reversedBinaryInteger(45) ➞ 45
// 45 = 101101 -> 101101 = 45

Notes

All values of n will be positive.

Solution:

1
2
3
4
5
6
7
8
function reversedBinaryInteger(num) {
  let binary = "";
  while (num) {
    binary += `${num % 2}`;
    num = Math.floor(num / 2);
  }
  return parseInt(binary, 2);
}