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);
Related
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?
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 1 year ago.
Improve this question
I am trying to make a recursive function for this parameters. Function should determine the nth element of the row
a_0 = 1
a_k = k*a_(k-1) + 1/k
k = 1,2,3...
I am trying really hard to do this but i cant get a result. Please help me to do this
I came up with this but it is not a recursive function. I can not do this as a recursive function
let a = 1
let k = 1
let n = 3
for (let i = 1; i<=n; i++){
a = i*a + 1/i
}
console.log(a)
Here's the recursive function you're looking for, it has two conditions:
k == 0 => 1
k != 0 => k * fn(k - 1) + 1/k
function fn(k) {
if(k <= 0) return 1;
return k * fn(k - 1) + 1/k;
}
console.log(fn(1));
console.log(fn(2));
console.log(fn(3));
console.log(fn(4));
Note: I changed the condition of k == 0 to k <= 0 in the actual function so it won't stuck in an infinite loop if you pass a negative k
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);
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 4 years ago.
Improve this question
I need to create an array of 20 random numbers in the range from -10 to 10 inclusive and Check whether all numbers match the parameters (from -10 to 10).
here is my code:
let array = [];
for (let i = 0; i < array.length; i++) {
array.push(Math.round((Math.random() * 21) - 10));
}
console.log(array);
It should be more like this
let array = [];
for(let i=0; i<20; i++){
array.push(Math.round((Math.random() * 21) - 10));
}
console.log(array);
This will do the trick,
var array = Array(20).fill().map(() => Math.round(Math.random() * 20) - 10);
console.log(array)
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 6 years ago.
Improve this question
How can I find an extra character between two strings in an optimal way.
Ex1: S1 - 'abcd', S2 - 'abcxd', output - 'x'
Ex2: S1 - '100001', S2 - '1000011', output - '1'
We can do this by traversing linearly and comparing each character in O(n). I want this to be done in much more optimal way, say in O(logn)
Baseline method (O(n)): Just comparing chars and narrowing in on both sides each cycle.
function findDiffChar(base, baseExtraChar) {
let extraLastIndex = base.length;
let lastIndex = extraLastIndex - 1;
for (let i = 0; i < extraLastIndex / 2; i++) {
console.log(`Loop: ${i}`);
if (base[i] !== baseExtraChar[i])
return baseExtraChar[i];
if (base[lastIndex - i] !== baseExtraChar[extraLastIndex - i])
return baseExtraChar[extraLastIndex - i];
}
return false;
}
console.log(findDiffChar('FOOOOOAR', 'FOOOOOBAR')); // B
Improved method using binary search (O(log n)): Compare halves until you've narrowed it down to one character.
function findDiffChar(base, baseExtraChar) {
if (baseExtraChar.length === 1) return baseExtraChar.charAt(0);
let halfBaseLen = Number.parseInt(base.length / 2) || 1;
let halfBase = base.substring(0,halfBaseLen);
let halfBaseExtra = baseExtraChar.substring(0,halfBaseLen);
return (halfBase !== halfBaseExtra)
? findDiffChar(halfBase, halfBaseExtra)
: findDiffChar(base.substring(halfBaseLen),baseExtraChar.substring(halfBaseLen));
}
console.log(findDiffChar('FOOOOAR', 'FOOOOBAR')); // B
console.log(findDiffChar('---------', '--------X')); // X
console.log(findDiffChar('-----------', '-----X-----')); // X
console.log(findDiffChar('------------', '---X--------')); // X
console.log(findDiffChar('----------', '-X--------')); // X
console.log(findDiffChar('----------', 'X---------')); // X