You have two arrays. One shows the names of the people names, while the other shows…
- 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 javascirpt
- Assistant
function assignPersonToJob(names, jobs) { const result = {};
for (let i = 0; i < names.length; i++) { const name = names[i]; const job = jobs[i]; result[name] = job; }
return result; }
const names = ["Annie", "Steven", "Lisa", "Osman"]; const jobs = ["Teacher", "Engineer", "Doctor", "Cashier"];
console.log(assignPersonToJob(names, jobs)); // Output: {Annie: "Teacher", Steven: "Engineer", Lisa: "Doctor", Osman: "Cashier"}
- 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 = 0; i < arr.length - 1; i++) { if (arr[i+1] % arr[i] !== 0) { return false; } } return true; }
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)); }
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 calculateBurglaryValue(stolenItems) { const totalValue = Object.values(stolenItems).reduce((acc, value) => acc + value, 0);
if (totalValue === 0) { return "Lucky you!"; } else { return totalValue; } }
const stolenItems1 = { tv: 30, skate: 20, stereo: 50, }; console.log(calculateBurglaryValue(stolenItems1)); // Output: 100
const stolenItems2 = { painting: 20000, }; console.log(calculateBurglaryValue(stolenItems2)); // Output: 20000
const stolenItems3 = {}; console.log(calculateBurglaryValue(stolenItems3)); // Output: "Lucky you!"
- User
Create a function that takes length and width and finds the perimeter of a rectangle.
Examples findPerimeter(6, 7) ➞ 26
findPerimeter(20, 10) ➞ 60
findPerimeter(2, 9) ➞ 22 Notes Don't forget to return the result. If you're stuck, find help in the Resources tab. If you're really stuck, find solutions in the Solutions tab. solve in javascript
- Assistant
function findPerimeter(length, width) { return (length + width) * 2; }
console.log(findPerimeter(6, 7)); // Output: 26 console.log(findPerimeter(20, 10)); // Output: 60 console.log(findPerimeter(2, 9)); // Output: 22
- User
Fix the code in the code tab to pass this challenge (only syntax errors). Look at the examples below to get an idea of what the function should do.
Examples cubes(3) ➞ 27
cubes(5) ➞ 125
cubes(10) ➞ 1000 Notes READ EVERY WORD CAREFULLY, CHARACTER BY CHARACTER! Don't overthink this challenge; it's not supposed to be hard. solve in javascript
- Assistant
function cubes(a) { return a ** 3; }
console.log(cubes(3)); // Output: 27 console.log(cubes(5)); // Output: 125 console.log(cubes(10)); // Output: 1000
- User
Create a function that returns a base-2 (binary) representation of a base-10 (decimal) string number. To convert is simple: ((2) means base-2 and (10) means base-10) 010101001(2) = 1 + 8 + 32 + 128.
Going from right to left, the value of the most right bit is 1, now from that every bit to the left will be x2 the value, value of an 8 bit binary numbers are (256, 128, 64, 32, 16, 8, 4, 2, 1).
Examples binary(1) ➞ "1" // 1*1 = 1
binary(5) ➞ "101" // 11 + 14 = 5
binary(10) ➞ "1010" // 12 + 18 = 10 Notes Numbers will always be below 1024 (not including 1024). The && operator could be useful. The strings will always go to the length at which the most left bit's value gets bigger than the number in decimal. If a binary conversion for 0 is attempted, return "0". solve in javascript
- Assistant
function binary(decimal) { if (decimal === 0) { return "0"; }
let binary = "";
for (let i = 512; i >= 1; i /= 2) { if (decimal >= i) { binary += "1"; decimal -= i; } else { binary += "0"; } }
return binary; }
console.log(binary(1)); // Output: "1" console.log(binary(5)); // Output: "101" console.log(binary(10)); // Output: "1010"