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" } 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; }
const names = ["Dennis", "Vera", "Mabel", "Annette", "Sussan"]; const jobs = ["Butcher", "Programmer", "Doctor", "Teacher", "Lecturer"];
console.log(assignPersonToJob(names, jobs));
- 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 strNum = num.toString(); // Convert number to string let sum = 0;
for (let i = 0; i < strNum.length; i++) { sum += parseInt(strNum[i]); // Convert each digit back to number and add to the sum }
const mean = Math.floor(sum / strNum.length); // Calculate the mean by dividing the sum by the number of digits
return mean; }
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)]; // Remove duplicate elements using Set data structure const sortedArr = uniqueArr.sort((a, b) => a - b); // Sort array in ascending order
return sortedArr; }
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
function uniqueSort(arr) { const uniqueArr = […uniqueSort(arr)]; const sortedArr = uniqueArr.sort((a, b) => a - b);
return sortedArr; }
console.log(uniqueSort([1, 2, 4, 3])); console.log(uniqueSort([1, 4, 4, 4, 4, 4, 3, 2, 1, 2])); console.log(uniqueSort([6, 7, 3, 2, 1])); here a bug solve them
- Assistant
The issue in the code you provided is with the line
const uniqueArr = […uniqueSort(arr)];. There is a typo, it should be...instead of…for the spread operator.Here's the corrected code:
function uniqueSort(arr) { const uniqueArr = [...new Set(arr)]; // Remove duplicate elements using Set data structure const sortedArr = uniqueArr.sort((a, b) => a - b); // Sort array in ascending order return sortedArr; } 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; } score += dice1 + dice2;}
return score; }
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
Create a function which takes two strings (p1 and p2 — which represent player 1 and 2) as arguments and returns a string stating the winner in a game of Rock, Paper, Scissors.
Each argument will contain a single string: "Rock", "Paper", or "Scissors". Return the winner according to the following rules:
Rock beats Scissors Scissors beats Paper Paper beats Rock If p1 wins, return the string "The winner is p1". If p2 wins, return the string "The winner is p2" and if p1 and p2 are the same, return "It's a draw".
Examples rps("Rock", "Paper") ➞ "The winner is p2"
rps("Scissors", "Paper") ➞ "The winner is p1"
rps("Paper", "Paper") ➞ "It's a draw" solve in javascript
- Assistant
function rps(p1, p2) { if ((p1 === "Rock" && p2 === "Scissors") || (p1 === "Scissors" && p2 === "Paper") || (p1 === "Paper" && p2 === "Rock")) { return "The winner is p1"; } else if ((p1 === "Scissors" && p2 === "Rock") || (p1 === "Paper" && p2 === "Scissors") || (p1 === "Rock" && p2 === "Paper")) { return "The winner is p2"; } else { return "It's a draw"; } }
console.log(rps("Rock", "Paper")); // Output: "The winner is p2" console.log(rps("Scissors", "Paper")); // Output: "The winner is p1" console.log(rps("Paper", "Paper")); // Output: "It's a draw"