This question already has answers here:
What is an off-by-one error and how do I fix it?
(6 answers)
Closed last month.
im doing a exercise of bills, tips and total of the all.
const calcTip = function (bill) {
return bill >= 50 && bill <= 300 ? bill * 0.15 : bill * 0.2;
};
const billsArray = [22, 295, 176, 440, 37, 105, 10, 1100, 86, 52];
const tipsArray = [];
const totalsArray = [];
for (let i = 0; i <= billsArray.length; i++) {
const tip = calcTip(billsArray[i]);
tipsArray.push(tip);
totalsArray.push(tip + billsArray[i]);
}
console.log(billsArray, tipsArray, totalsArray);
It's all right but in the browser console said that last one is not a number? someone knows why? do i need to use float?
it's because you didn't set condition <= exactly in for loop. it should be i<billsArray.length
It's because of out of array index i <= billsArray.length
can fixable with i < billsArray.length or i <= billsArray.length - 1
Related
I'm doing a JS course on Udemy and I'm stuck with a challenge and the course is really still quite basic so the code I'm writing is basic too.
const bills = [22, 295, 176, 440, 37, 105, 10, 1100, 86, 52];
let tips = [];
let totals = [];
const calcTip = function (bills) {
return bills >= 50 && bills <= 300 ? bills * 0.15 : bills * 0.2
}
// for (i = 0; i < bills.length; i++) {
// const newTips = [calcTip(bills[i])];
// tips.push(newTips);
// const newTotals = [(bills[i] + tips[i])];
// totals.push(newTotals);
// }
tips = [calcTip(bills[1])];
totals = [bills[1] + tips[1]];
console.log(bills, tips, totals);
At the minute the totals bit is only for the second item from bills as I wanted to know whether I do it okay, so I'd appreciate a tip why the totals results in NaN in my console. the bills and tips seem to be working fine.
Thank you!
the totals was meant to give me a sum of bill and tip
I'm working through a JavaScript challenge problem Find Numbers with the Same Amount of Divisors and have run into some trouble at the end of my code where a for loop is involved.
The problem:
Find all pairs of numbers between 1 and NMax that are diff numbers apart and share the same amount of divisors.
For example: For numbers between 1 and 50, there are 8 numbers share the same number of divisors: [[2,3], [14,15], [21,22], [26,27], [33, 34], [34, 35], [38, 39], [44, 45]]
In my code below, count_pairsInt(1,50) will return 8, but count_pairsInt (3,100) returns TypeError: Cannot read properties of undefined (reading '1').
I'm almost certain something has gone awry in the last for loop, but I can't quite get my mind around what it is. Could someone help me out?
function countPairsInt(diff, nMax) {
const numbers = [];
for (let i=1; i<=nMax; i++) {
numbers.push(i);
}
// divisors loops over each number passed in and returns the number of divisors for that number
function divisors(num) {
let divs = [];
for (let i=1; i<=num; i++) {
if (num % i === 0) divs.push(i);
}
return divs;
}
// create an array of arrays, each subarray contains the number and it's number of divisors by passing map over the numbers array.
const numsAndDivs = numbers.map(x=> [x, divisors(x).length]);
let equalDivs = 0;
for (let i=1; i<numsAndDivs.length-1; i++) {
if (numsAndDivs[i][1] === numsAndDivs[i+diff][1] ){
equalDivs++;
}
}
return equalDivs
}
countPairsInt(1, 50); // returns 8
countPairsInt(3, 100) // should return 7
You forgot to add a simple check that i + diff must be less than numsAndDivs.length. it was getting 100 in your case and there was no array of index 100 in numsAndDivs. hence the error.
if (i+ diff < numsAndDivs.length && numsAndDivs[i][1] === numsAndDivs[i+diff][1] ){
equalDivs++;
}
The have a solution to a problem that looks like this. The input for instances is meant to me an integer and an array. Given the following input
console.log(finalInstances(13, [40, 89, 79, 76, 66, 60, 8, 90, 19, 39, 53, 30, 93]))
function finalInstances(instances, averageUtil) {
// Write your code here
for (var i = 0; i < averageUtil.length; i++) {
if (averageUtil[i] < 25) {
if (instances > 1) {
i += 9
console.log(i)
instances = Math.ceil((instances / 2));
}
} else if (averageUtil[i] > 60) {
if (instances * 2 < (2 * 10 ^ 8)) {
instances = instances * 2;
i = i + 9;
}
}
}
return instances
}
The output for this particular test case should be 52. However, I always get 26 (half of 52). After debugging my solution, I realize that the second IF statement in my solution doesn't work as intended. It only perform instances = instances * 2 the first time we enter the statement, but no longer after (hence why my return is 26 and not 52). Can someone explain what I am doing wrong here or how I should alter my code?
Thanks!!
I'm not sure what this is supposed to do, but I suspect
if (instances * 2 < (2 * 10 ^ 8))
is not doing what you want. ^ is bitwise XOR and has lower precedence than *, so (2 * 10 ^ 8) is 20 XOR 8 which is 28.
Use 2e8 to get 2 with 8 zeroes after it.
console.log(finalInstances(13, [40, 89, 79, 76, 66, 60, 8, 90, 19, 39, 53, 30, 93]))
function finalInstances(instances, averageUtil) {
// Write your code here
for (var i = 0; i < averageUtil.length; i++) {
if (averageUtil[i] < 25) {
if (instances > 1) {
i += 9
console.log(i)
instances = Math.ceil((instances / 2));
}
} else if (averageUtil[i] > 60) {
if (instances * 2 < 2e8) {
instances = instances * 2;
i = i + 9;
}
}
}
return instances
}
On line 6 you are increasing the index variable by 9. That means that you are skipping over all of the elements of the array that are greater than 60.
How can I find all the existing factors between two number?
for example all the factors of 5 that are between 10 and 1000.
One thing I can think of is like this:
function getFactors(factor, numbers) {
let factors = new Array()
for (var i = numbers[0] ; i <= numbers[1] ; i++) {
if (i % factor == 0) factors.push(i)
}
return factors
}
console.log(getFactors(5, [50, 100]))
But the problem is that I have a big number range ([0, 10000]) and a small factor 0.01. so I think doing this using my solution will be too much in terms of performance (i need to do this check every tick).
so I was wondering what is the better way to do this?
You can achieve same thing without checking for every number between numbers[0] & numbers[1]. What you need basically is to get every multiple of factor between numbers[0] & numbers[1]. Below code does it:
function getFactors(factor, numbers) {
let factors = new Array();
let start = numbers[0]/factor;
let end = numbers[1]/factor;
for(var i=start; i<= end; i++) {
factors.push(factor*i);
}
return factors
}
console.log(getFactors(5,[50,100]))
//[50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
This question already has answers here:
Want to produce random numbers between 1-45 without repetition
(4 answers)
Closed 4 years ago.
I need help with my code. I want to get 10 random numbers every time it rolls and it can't have duplicated, this is my code:
for(let j = 1; j <= 21; j++) {
const number = (Math.floor((Math.random() * j) + 1))
const genNumber = array.indexOf(number);
if (genNumber === -1) {
array.push(number);
}
}
I have no idea how I can get exactly 10 numbers every time, anyway it can be written with js or jquery it doesnt metter for me. Hope I can get help here.
I don't really understand what your code is intended to do, but to get exactly 10 unique random numbers I'd use a Set and loop until it's filled. I have no idea why you loop 21 times for 10 items though...
let s = new Set();
while (s.size < 10) {
s.add((Math.floor(Math.random() * 21 /* ??? */) + 1));
}
console.log([...s]);
You're almost there, but instead of using a for loop, use a while loop that continues as long as there are less than 10 things in the array:
const array = [];
const j = 21; // Pick numbers between 1 and 21 (inclusive)
while (array.length < 10) {
const number = (Math.floor((Math.random() * j) + 1))
const genNumber = array.indexOf(number);
if (genNumber === -1) {
array.push(number);
}
}
console.log(array);