how to resolve sum of prime-indexed elements? - javascript

Good morning, am getting stuck with this kata, anyone can explain me ?
In this kata i have to return the sum of elements occupying prime-numbered indices.
I started my code like that, but didn't know what to do next. THANK YOU in advance.
function total(arr) {
for (let i = 2; i < arr.length; i++) {}
};

Please use ; after breaking condition which is i < arr.length here for (let i = 2; i < arr.length, i++) {}
You can loop over arr the way you are doing here and validate if index at which you are looping is prime itself.
If 2nd step is met you can add the value into summation var sum
return sum.
Please follow this thread to find know more about calculating if value is prime. Number prime test in JavaScript
function isPrime(num) {
for(var i = 2; i < num; i++)
if(num % i === 0) return false;
return num > 1;
}
function total(arr) {
var sum = 0;
for (let i = 2; i < arr.length; i++) {
if(isPrime(i)) {
sum = sum + arr[i]
}
}
return sum;
}

Related

CodeSignal sumInRange challenge in JavaScript

I'm working on this CodeSignal exercise that says:
You have an array of integers nums and an array queries, where queries[i] is a pair of indices (0-based). Find the sum of the elements in nums from the indices at queries[i][0] to queries[i][1] (inclusive) for each query, then add all of the sums for all the queries together. Return that number modulo 10^9 + 7.
This is my code:
function solution(nums, queries) {
let accumulator = 0;
let M = 1000000007;
for(let i = 0; i < queries.length; i++){
accumulator += nums.slice(queries[i][0],queries[i][1]+1).reduce((a,b) => a+b);
}
return accumulator < 0 ? ((accumulator % M) + M) % M : accumulator%M;
}
It works perfectly, BUT the penultimate hidden test throws a timeout, and I'm out of ideas on how to make this faster.
Thanks in advance for any help you may provide.
PS: if you're wondering about the difference using modulo at the end, it's because it seems JS has a bug when using negative numbers.
As tflave pointed out, using a prefix sum made the code perform faster, and it didn't timeout. Here's the code if anyone needs it:
let pre = new Array(1000,0);
function preCompute(nums)
{
let n = nums.length;
pre[0] = nums[0];
for (let i = 1; i < n; i++) {
pre[i] = nums[i] + pre[i - 1];
}
}
function solution(nums, queries)
{
preCompute(nums);
let accumulator = 0;
let M = 1000000007;
for(let i = 0; i < queries.length; i++){
if (queries[i][0] === 0) {
accumulator += pre[queries[i][1]];
} else {
accumulator += pre[queries[i][1]] - pre[queries[i][0] - 1];
}
}
return accumulator < 0 ? ((accumulator % M) + M) % M : accumulator%M;
}

Sum of Digits works in console but not when returned

So I'm doing a codewars challenge and I have no clue why my code isn't working. I'm a beginner so please don't hate on me.
This is my code:
function digital_root(n) {
let str = n.toString()
let arr = []
let sum = 0
for (let i = 0; i < str.length; i++) {
arr.push(str.charAt(i))
}
for (let i = 0; i < str.length; i++) {
sum += Number(arr[i])
}
let sumStr = sum.toString()
if (sumStr.length > 1) {
digital_root(sum)
} else if (sumStr.length == 1) {
return sum
}
}
It works when I console.log it but not when I return the value. I'm trying to learn recursion. Thanks for the help!
You need to return digital_root(sum) too, if sumStr.length > 1 in order to access recursive returned value.
you have to write return digital_root(sum) instead of just digital_root(sum).
check below:
function digital_root(n) {
let str = n.toString()
let arr = []
let sum = 0
for (let i = 0; i < str.length; i++) {
arr.push(str.charAt(i))
}
for (let i = 0; i < str.length; i++) {
sum += Number(arr[i])
}
let sumStr = sum.toString()
if (sumStr.length > 1) {
return digital_root(sum)
} else if (sumStr.length == 1) {
return sum
}
}
console.log("Digital Root :", digital_root('123456789'));
Ok, it seems you are missing dealing with the return value of digital_root when you call it recursively. See added "return" statement below.
function digital_root(n) {
let str = n.toString()
let arr = []
let sum = 0
for (let i = 0; i < str.length; i++) {
arr.push(str.charAt(i))
}
for (let i = 0; i < str.length; i++) {
sum += Number(arr[i])
}
let sumStr = sum.toString()
if (sumStr.length > 1) {
// ***** You need to deal with the return value of digital_root when you call it.
return digital_root(sum)
} else if (sumStr.length == 1) {
return sum
}
}
However, while JavaScript's functional coding style does support recursive functions, we need to be aware that most JavaScript compilers are not currently optimized to support them safely. Recursion is best applied when you need to call the same function repeatedly with different parameters from within a loop.
Please read this,
https://www.sitepoint.com/recursion-functional-javascript/#:~:text=However%2C%20while%20JavaScript's%20functional%20coding,parameters%20from%20within%20a%20loop.

SUM of all elements of a two-dimensional array [duplicate]

This question already has answers here:
How to return values in javascript
(8 answers)
Closed 2 years ago.
I am trying to solve a challenge from jshero.net. The challenge is:
Write a function sum that calculates the sum of all elements of a
two-dimensional array. sum([[1, 2], [3]]) should return 6.
For this one I need to use a nested loop. The best solution I could come up with is:
function sum(num){
let mySum= [num.length]
var sum = 0;
for (var i = 0; i > mySum; i++) {
for (var j = 0; j > mySum; j++) {
sum =mySum[[i]+[j]];
}
}
}
But when I run the code I get the following error:
sum([[1]]) does not return 1, but undefined.
Test-Error! Correct the error and re-run the tests!
Do you guys have any ideea how to solve this?
I think the function should look something like this:
function sum(arr) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
sum += arr[i][j];
}
}
return sum;
}
arr=[[1,2,3,4],5,6,[7,8],9]
var sum=0;
for(var d1 of arr)
if(d1.length) // undefined if not array||number
for(var d2 of d1)
sum+=d2;
else
sum+=d1;
// 45

returning only the first value of the largest element

I'm trying to return the largest element in the array. With strings this means the longest string. How do I return only the first instance of the largest element.
My code:
function getLongestElement(arr) {
var max = "";
var counter = 0;
for (var i = 0; i < arr.length; i++){
if (arr[i].length > counter) max = arr[i]
}
return max;
}
getLongestElement(['one', 'two', 'three', "thre1"]); // "thre1" not "three".
I'm not quite sure whats wrong with this code. No matter what the largest value is it only returns the last element in the array. Help?
counter is initialized to 0, but you never change its value so the if statement with arr[i].length > counter is always true (unless arr[i].length == 0). To fix it you need to keep track of the largest element of the array inside the loop:
// I renamed counter to maxLength for readability
function getLongestElement(arr) {
var max;
var maxLength = -1;
for (var i = 0; i < arr.length; i++){
if (arr[i].length > maxLength){
maxLength = arr[i].length;
max = i;
}
}
return arr[max];
}
First, good alghoritm should make no assumptions. That means your max shouldn't start from "", but using first array's element. You also don't edit your counter value, that's your main problem. But it is redundant and you can write this function without counter.
function getLongestElement(arr) {
if (arr.length < 1) return /* Some Exception */;
var max = arr[0];
for (var i = 1; i < arr.length; i++) {
if (arr[i].length > max.length) max = arr[i];
}
return max;
}
You forgot to update counter
function getLongestElement(arr) {
var max = "";
var counter = 0;
for (var i = 0; i < arr.length; i++){
if (arr[i].length > counter) {
max = arr[i];
counter = max.length;
}
}
return max;
}
console.log(getLongestElement(['one', 'two', 'three', "thre1"])); // "thre1" not "three".
If you're looking for a pragmatic solution, I'd suggest lodash's _.maxBy:
_.maxBy(['one', 'two', 'three', "thre1"], function(str) {
return str.length;
})
If you're looking for a theoretical solution for the sake of learning,
function getLongestElement(arr) {
var max;
var counter = 0;
for (var i = 0; i < arr.length; i++){
if (arr[i].length > counter) max = arr[i]
counter = Math.max(arr[i].length, counter)
}
return max;
}
The key point here is to make sure you're updating the counter variable to whatever the currently longest length is.

Sum of Primes using Sieve of Eratosthenes can't find bug

I'm working in JavaScript and this is a bit confusing because the code is returning the correct sum of primes. It is working with larger numbers. There is a bug where for 977 it returns the sum of primes for 976, which is 72179, instead of the sum for 977 which is 73156. Everything I've test so far has come back correctly.
function sumPrimes(num) {
var sum = 0;
var count = 0;
var array = [];
var upperLimit = Math.sqrt(num);
var output = [];
for (var i = 0; i < num; i++) {
array.push(true);
}
for (var j = 2; j <= upperLimit; j++) {
if (array[j]) {
for (var h = j * j; h < num; h += j) {
array[h] = false;
}
}
}
for (var k = 2; k < num; k++) {
if (array[k]) {
output.push(k);
}
}
for (var a = 0; a < output.length; a++) {
sum += output[a];
count++;
}
return sum;
}
sumPrimes(977);
The problem stems from the fact that your "seive" Array is indexed from 0, but your algorithm assumes that array[n] represents the number n.
Since you want array[n]===true to mean that n is prime, you need an Array of length 978 if you want the last item to be indexed as array[977] and mean the number 977.
The issue seems to be fixed when I change all instances of < num to < num+1.

Categories