Javascript. Object.values(...).reduce [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Can anyone explain in detail how this piece of code works?
const yourAmazingFunction = (salaries) => Object.values(salaries).reduce((acc, el) => acc + el, 0);
specifically this:
((acc, el) => acc + el, 0);
Here's all the code:
const nonEmptySalaries = {
John: 100,
Ann: 160,
Pete: 130
};
const emptySalaries = {};
const yourAmazingFunction = (salaries) => Object.values(salaries).reduce((acc, el) => acc + el, 0);
const firstResult = yourAmazingFunction(nonEmptySalaries)
const secondResult = yourAmazingFunction(emptySalaries)
console.log(firstResult); // 390
console.log(secondResult); // 0 ```

Take all of the values (in this case an array [100, 160, 130])
Loop over the array values, setting acc to the result of the last call of the callback, or 0 if this is the first call, and el to the current element. Basically add together each number (callback(0, 100) then callback(100, 160) then callback(260, 130) to return 390).

Related

Maybe someone knows a simple algorithm for finding the number of '0' elements that are between '1' elements in an array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I need a simple algorithm to find the number of occurrences of 0 between 1 in an array, for example, after performing certain operations on these arrays:
var mas1 = [0,1,0,0,1]
var mas2 = [1,0,0,0,1]
var mas3 = [0,1,0,1,0]
var mas4 = [0,0,0,0,1]
var mas4 = [1,0,1,0,1]
We have to get:
result = 2
result = 3
result = 1
result = 0
result = 2
We can have any number of 1's or 0's in an unlimited array
Any ideas?
You can continuously use indexOf to find the next 1 while accumulating the size of ranges in between consecutive ones.
let arr = [0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1];
let count = 0;
for(let i = arr.indexOf(1); i != -1; ){
const next = arr.indexOf(1, i + 1);
if (next !== -1) count += next - i - 1;
i = next;
}
console.log(count);

Testing a partial prorogram: Why is Modulus function not working? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
const marks = [80];
const grade = ['A', 'B', 'C', 'D', 'F'];
let sum = 0;
console.log(checkGrade(marks));
function checkGrade(array) {
for (let item of array)
sum += item;
const avg = sum / array.length;
let gradePoint = Math.floor((100 - avg) / 10);
return gradePoint % 10;
}
as Your code, the avg is = 80
and (100-avg) / 10 = 2 which is gradePoint
and you are returning gradePoint % 10
that means 2 % 10
which is returning 2.
and it is working perfectly. Where is the issue?

Need help for an algo [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I make a site where the color of the div is randomly generating compared to this array:
let pastelColor = [
'ffaacc','ffbbcc','ffcccc','ffddcc','ffeecc','ffffcc','ffaadd','ffbbdd','ffccdd','ffdddd','ffeedd','ffffdd',
'ffaaee','ffbbee','ffccee','ffddee','ffeeee','ffffee','ffaaff','ffbbff','ffccff','ffddff','ffeeff','ffffff',
'ccaaff','ccbbff','ccccff','ccddff','cceeff','ccffff','ccaaee','ccbbee','ccccee','ccddee','cceeee','ccffee',
'ccaadd','ccbbdd','ccccdd','ccdddd','cceedd','ccffdd','ccaacc','ccbbcc','cccccc','ccddcc','cceecc','ccffcc'
]
I tried several Algo with Case Switch but the function took too much room, I was wondering in curiosity if an algo would be able to generate this array ?
Thank you in advance for those who answer
if an algo would be able to generate this array ?
Sure. If you need that array in that exact order:
let pastelColor = Array.from("abcdef".repeat(8), (ch, i) =>
(([a,...r]) => a+a+ch+ch+r[~~(i/6)%4].repeat(2))(i<24 ? "fcdef" : "cfedc")
);
console.log(pastelColor);
If you are not really interested in the array with color codes, but only in the generation of a random color from it, then:
let pick = str => str[Math.floor(Math.random() * str.length)].repeat(2);
let pastelColor = pick("cf") + pick("abcdef") + pick("cdef");
console.log(pastelColor);
You need to fill the array with the converted hexadecimal color with randomly generated integers ranging from 0 to 255 like this:
const getRandomInt = (min, max) => {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
const getRandomColorUnit = () => {
const color = getRandomInt(0, 256).toString(16);
if (color.length < 2) return '0' + color;
return color;
};
const getRandomColor = () => `#${getRandomColorUnit()}${getRandomColorUnit()}${getRandomColorUnit()}`;
const colors = new Array(100).fill(0).map(getRandomColor);
console.log(colors);

How to decrease numbers from an array in javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
There are numbers in an array like 54321
array = ["54321"] ;
Now how can I print in console
54321
5432
543
54
5
I don't want to print it manually, I want to print it dynamically..
Thanks in advance.
const array = ["54321"];
const print = ([x]) => {
if (x.length === 0) return;
console.log(x);
print([x.slice(0, -1)]);
}
print(array)
You can split the string into array and the use reduce function
ler arr = array[0].split('');
let result = arr.reduce((acc,i) => {
acc.newarr.push(arr.slice(0,acc.counter).join(''));
acc.counter--;
return acc;
},
{newarr:[],counter:5});
console.log(result);
const array = ["54321"];
const un_print = ([str]) =>
Array.from(
{ length: str.length },
(_, i) => (i + 1) + '. ' + str.slice(0, str.length - i)
)
.forEach((v) => console.log(v));
un_print(array);

How can loop be rewritten as anonymous function? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
how can this type of loop be overwritten
let num - fieldOffset[0];
for (let i = 1; i < fieldOffset.length; i++) {
if (fieldOffset[i] < fieldOffset[i - 1]) {
num += fieldOffset[i];
}
}
as an anonymous function of the following type:
const reducer = (accumulator, currentValue) => accumulator + currentValue;
array1.reduce(reducer)
Using the third (optional) argument of Array.prototype.reduce should do the trick:
num = fieldOffset.reduce((acc, value, index) => acc += (index && (value > fieldOffset[index - 1])) ? value : 0, 0);

Categories