Create a function that returns the mean of all digits. Examples mean(42) ➞ 3 mean(12345)…
- User
Create a function that returns the mean of all digits.
Examples mean(42) ➞ 3
mean(12345) ➞ 3
mean(666) ➞ 6 Notes The mean of all digits is the sum of digits / how many digits there are (e.g. mean of digits in 512 is (5+1+2)/3(number of digits) = 8/3=2). The mean will always be an integer. solve in javascript
- Assistant
function mean(num) { const digits = Array.from(String(num), Number); const sum = digits.reduce((total, digit) => total + digit, 0); return Math.floor(sum / digits.length); }// Example usage: console.log(mean(42)); // Output: 3 console.log(mean(12345)); // Output: 3 console.log(mean(666)); // Output: 6
- User
Given an array of numbers, write a function that returns an array that...
Has all duplicate elements removed. Is sorted from least to greatest value. Examples uniqueSort([1, 2, 4, 3]) ➞ [1, 2, 3, 4]
uniqueSort([1, 4, 4, 4, 4, 4, 3, 2, 1, 2]) ➞ [1, 2, 3, 4]
uniqueSort([6, 7, 3, 2, 1]) ➞ [1, 2, 3, 6, 7] solve in javascript
- Assistant
function uniqueSort(arr) { const uniqueArr = [...new Set(arr)]; // Using Set to remove duplicates return uniqueArr.sort((a, b) => a - b); // Sorting in ascending order }// Example usage: console.log(uniqueSort([1, 2, 4, 3])); // Output: [1, 2, 3, 4] console.log(uniqueSort([1, 4, 4, 4, 4, 4, 3, 2, 1, 2])); // Output: [1, 2, 3, 4] console.log(uniqueSort([6, 7, 3, 2, 1])); // Output: [1, 2, 3, 6, 7]
- User
John is playing a dice game. The rules are as follows.
Roll two dice. Add the numbers on the dice together. Add the total to your overall score. Repeat this for three rounds. But if you roll DOUBLES, your score is instantly wiped to 0 and your game ends immediately!
Create a function which takes in a matrix as input, and return John's score after his game has ended.
Examples diceGame([[1, 2], [3, 4], [5, 6]]) ➞ 21
diceGame([[1, 1], [5, 6], [6, 4]]) ➞ 0
diceGame([[4, 5], [4, 5], [4, 5]]) ➞ 27 Notes Ignore all other arrays in the matrix if a throw happens to be doubles and go straight to returning 0. John only has two dice and will always give you outcomes for three rounds. solve in javascript
- Assistant
function diceGame(matrix) { let score = 0; for (let i = 0; i < matrix.length; i++) { const dice1 = matrix[i][0]; const dice2 = matrix[i][1]; if (dice1 === dice2) { return 0; // If doubles are rolled, instantly return 0 } score += dice1 + dice2; } return score; }// Example usage: console.log(diceGame([[1, 2], [3, 4], [5, 6]])); // Output: 21 console.log(diceGame([[1, 1], [5, 6], [6, 4]])); // Output: 0 console.log(diceGame([[4, 5], [4, 5], [4, 5]])); // Output: 27
- User
Write a function that takes three arguments (x, y, z) and returns an array containing x subarrays (e.g. [[], [], []]), each containing y number of item z.
x Number of subarrays contained within the main array. y Number of items contained within each subarray. z Item contained within each subarray. Examples matrix(3, 2, 3) ➞ [[3, 3], [3, 3], [3, 3]]
matrix(2, 1, "edabit") ➞ [["edabit"], ["edabit"]]
matrix(3, 2, 0) ➞ [[0, 0], [0, 0], [0, 0]] Notes The first two arguments will always be integers. The third argument is either a string or an integer. solve in javascript
- Assistant
function matrix(x, y, z) { const subarray = new Array(y).fill(z); const result = new Array(x).fill(subarray); return result; }// Example usage: console.log(matrix(3, 2, 3)); // Output: [[3, 3], [3, 3], [3, 3]] console.log(matrix(2, 1, "edabit")); // Output: [["edabit"], ["edabit"]] console.log(matrix(3, 2, 0)); // Output: [[0, 0], [0, 0], [0, 0]]
- User
You have two arrays. One shows the names of the people names, while the other shows their occupation jobs. Your task is to create an object displaying each person to their respective occupation.
Names Jobs Annie Teacher Steven Engineer Lisa Doctor Osman Cashier Example const names = ["Dennis", "Vera", "Mabel", "Annette", "Sussan"] const jobs = ["Butcher", "Programmer", "Doctor", "Teacher", "Lecturer"]
assignPersonToJob(names, jobs) ➞ { Dennis: "Butcher", Vera: "Programmer", Mabel: "Doctor", Annette: "Teacher", Sussan: "Lecturer" } Notes The two arrays have the same length. The index of a name in the names array is the same as the index of the person's job in the jobs array, as shown in the table above. Check Resources for some useful information that can help with this challenge. solve in javascript
- Assistant
function assignPersonToJob(names, jobs) { const result = {}; for (let i = 0; i < names.length; i++) { result[names[i]] = jobs[i]; } return result; }// Example usage: const names = ["Dennis", "Vera", "Mabel", "Annette", "Sussan"]; const jobs = ["Butcher", "Programmer", "Doctor", "Teacher", "Lecturer"]; console.log(assignPersonToJob(names, jobs)); // Output: {Dennis: "Butcher", Vera: "Programmer", Mabel: "Doctor", Annette: "Teacher", Sussan: "Lecturer"}
- User
A factor chain is an array where each previous element is a factor of the next consecutive element. The following is a factor chain:
[3, 6, 12, 36]
// 3 is a factor of 6 // 6 is a factor of 12 // 12 is a factor of 36 Create a function that determines whether or not an array is a factor chain.
Examples factorChain([1, 2, 4, 8, 16, 32]) ➞ true
factorChain([1, 1, 1, 1, 1, 1]) ➞ true
factorChain([2, 4, 6, 7, 12]) ➞ false
factorChain([10, 1]) ➞ false solve in javascript
- Assistant
function factorChain(arr) { for (let i = 1; i < arr.length; i++) { if (arr[i] % arr[i - 1] !== 0) { return false; } } return true; }// Example usage: console.log(factorChain([1, 2, 4, 8, 16, 32])); // Output: true console.log(factorChain([1, 1, 1, 1, 1, 1])); // Output: true console.log(factorChain([2, 4, 6, 7, 12])); // Output: false console.log(factorChain([10, 1])); // Output: false
- User
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 false, 0, "" (empty string), null, undefined, and NaN; everything else is truthy. You will always be supplied with at least one parameter. solve in javascript
- Assistant
function allTruthy(...args) { return args.every((arg) => Boolean(arg)); }// Example usage: console.log(allTruthy(true, true, true)); // Output: true console.log(allTruthy(true, false, true)); // Output: false console.log(allTruthy(5, 4, 3, 2, 1, 0)); // Output: false
- User
You just returned home to find your mansion has been robbed! Given an object of the stolen items, return the total amount of the burglary (number). If nothing was robbed, return the string "Lucky you!".
Examples const stolenItems = { tv: 30, skate: 20, stereo: 50, } ➞ 100
const stolenItems = { painting: 20000, } ➞ 20000
const stolenItems = {} ➞ "Lucky you!" Notes The item value is always positive. This is my first experience in creating a challenge. I started to learn testing so I decided to give it a go here at Edabit as well. Hope I did it right... Suggestions are very welcome! This series is part of a collection that focuses on objects. If you are interested in following the breath-taking narrative skills of yours truly or just do some object focused challenges (the challenges are ordered in ascending difficulty order), you can more easily do that there. solve in javascript
- Assistant
function calculateLosses(stolenItems) { const totalLoss = Object.values(stolenItems).reduce( (sum, currentValue) => sum + currentValue, 0 ); return totalLoss > 0 ? totalLoss : "Lucky you!"; }// Example usage: const stolenItems1 = { tv: 30, skate: 20, stereo: 50, }; console.log(calculateLosses(stolenItems1)); // Output: 100
const stolenItems2 = { painting: 20000, }; console.log(calculateLosses(stolenItems2)); // Output: 20000
const stolenItems3 = {}; console.log(calculateLosses(stolenItems3)); // Output: "Lucky you!"
- User
Create a function which concatenates the number 7 to the end of every chord in an array. Ignore all chords which already end with 7.
Examples jazzify(["G", "F", "C"]) ➞ ["G7", "F7", "C7"]
jazzify(["Dm", "G", "E", "A"]) ➞ ["Dm7", "G7", "E7", "A7"]
jazzify(["F7", "E7", "A7", "Ab7", "Gm7", "C7"]) ➞ ["F7", "E7", "A7", "Ab7", "Gm7", "C7"]
jazzify([]) ➞ [] Notes Return an empty array if the given array is empty. You can expect all the tests to have valid chords. solve in javascript
- Assistant
function jazzify(chords) { return chords.map((chord) => chord.endsWith("7") ? chord : chord + "7"); }// Example usage: console.log(jazzify(["G", "F", "C"])); // Output: ["G7", "F7", "C7"] console.log(jazzify(["Dm", "G", "E", "A"])); // Output: ["Dm7", "G7", "E7", "A7"] console.log(jazzify(["F7", "E7", "A7", "Ab7", "Gm7", "C7"])); // Output: ["F7", "E7", "A7", "Ab7", "Gm7", "C7"] console.log(jazzify([])); // Output: []
- User
Create a function that takes an array of numbers and return "Boom!" if the digit 7 appears in the array. Otherwise, return "there is no 7 in the array".
Examples sevenBoom([1, 2, 3, 4, 5, 6, 7]) ➞ "Boom!" // 7 contains the number seven.
sevenBoom([8, 6, 33, 100]) ➞ "there is no 7 in the array" // None of the items contain 7 within them.
sevenBoom([2, 55, 60, 97, 86]) ➞ "Boom!" // 97 contains the number seven. solve in javascript
- Assistant
function sevenBoom(arr) { for (let i = 0; i < arr.length; i++) { if (arr[i].toString().includes("7")) { return "Boom!"; } } return "there is no 7 in the array"; }// Example usage: console.log(sevenBoom([1, 2, 3, 4, 5, 6, 7])); // Output: "Boom!" console.log(sevenBoom([8, 6, 33, 100])); // Output: "there is no 7 in the array" console.log(sevenBoom([2, 55, 60, 97, 86])); // Output: "Boom!"
- User
Create a function that returns the frequency distribution of an array. This function should return an object, where the keys are the unique elements and the values are the frequency in which those elements occur.
Examples getFrequencies(["A", "B", "A", "A", "A"]) ➞ { A: 4, B: 1 }
getFrequencies([1, 2, 3, 3, 2]) ➞ { "1": 1, "2": 2, "3": 2 }
getFrequencies([true, false, true, false, false]) ➞ { true: 2, false: 3 }
getFrequencies([]) ➞ {} Notes If given an empty array, return an empty object (see example #4). The object should be in the same order as in the input array. solve in javascript
- Assistant
function getFrequencies(arr) { const frequency = {}; for (let i = 0; i < arr.length; i++) { const element = arr[i]; frequency[element] = frequency[element] ? frequency[element] + 1 : 1; } return frequency; }// Example usage: console.log(getFrequencies(["A", "B", "A", "A", "A"])); // Output: { A: 4, B: 1 } console.log(getFrequencies([1, 2, 3, 3, 2])); // Output: { "1": 1, "2": 2, "3": 2 } console.log(getFrequencies([true, false, true, false, false])); // Output: { true: 2, false: 3 } console.log(getFrequencies([])); // Output: {}
- User
Create a function that returns true if there's at least one prime number in the given range (n1 to n2 (inclusive)), false otherwise.
Examples primeInRange(10, 15) ➞ true // Prime numbers in range: 11, 13
primeInRange(62, 66) ➞ false // No prime numbers in range.
primeInRange(3, 5) ➞ true // Prime numbers in range: 3, 5 Notes n2 is always greater than n1. n1 and n2 are always positive. 0 and 1 aren't prime numbers. solve in javascript
- Assistant
function primeInRange(n1, n2) { for (let num = n1; num <= n2; num++) { if (isPrime(num)) { return true; } } return false; } // Helper function to check if a number is prime function isPrime(num) { if (num < 2) { return false; } for (let i = 2; i <= Math.sqrt(num); i++) { if (num % i === 0) { return false; } } return true; }// Example usage: console.log(primeInRange(10, 15)); // Output: true console.log(primeInRange(62, 66)); // Output: false console.log(primeInRange(3, 5)); // Output: true
- User
Create a function that finds how many prime numbers there are, up to the given integer.
Examples primeNumbers(10) ➞ 4 // 2, 3, 5 and 7
primeNumbers(20) ➞ 8 // 2, 3, 5, 7, 11, 13, 17 and 19
primeNumbers(30) ➞ 10 // 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29 solve in javascript
- Assistant
function primeNumbers(num) { let count = 0; for (let i = 2; i <= num; i++)