Backtracking issue (I think?) - Javascript - javascript

I like to challenge myself just for fun and I've come across to a problem which I just simply can not solve on my own. I've tried and tried, but I can not find a solution to it and that's why I came here to ask from you.
So let's assume we have three different arrays:
var y1 = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
var y2 = [ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ];
var y3 = [ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 ];
We also have five different integers:
var z1 = 200;
var z2 = 400;
var z3 = 600;
var z4 = 800;
var z5 = 1000;
How am I able to see the largest possible number, when the formula looks something like this:
x = z1*y1[0] + z2*y1[0] + z3*y2[0] + z4*y2[0] * z5*y3[0];
... and I only have 25 points to use to replace the zeros in the equation?
For example:
z1*y1[5] + z2*y1[5] + z3*y2[5] + z4*y2[5] * z5*y3[5] = 32000 (5+5+5+5+5 = 25)
z1*y1[0] + z2*y1[10] + z3*y2[0] + z4*y2[10] * z5*y3[5] = 35000 (0+10+0+10+5 = 25)
I think I know how it should be done (backtracking?) but I just can't figure out how to o it exactly. I also couldn't find this type of a problem from the Internet, so if you know where to look that would be great as well because I'm not looking for a direct answer, I'd like to learn.
So any help would be much appreciated!

You can think of it as having five machines(m) that needs to perform one job(n), of varying processing times(z), and y denote when to schedule each job.
Assuming a ascending heuristic:
max(25) = z1*y1[0] + z2*y1[0] + z3*y2[5] + z4*y2[10] * z5*y3[10] Result: 480006000

Related

why JavaScript is behaving differently

why it is showing me 36 even though the minimum number is 27
var combination = [27, 36]
for (let x in combination) {
if (combination[x] < 50) {
var min = Math.min(combination[x])
}
}
console.log(min)
i tried this multiple ways like
var combination = [27, 30, 40, 44, 3, 239, 329, 2, 5, 20923, 96]
for (let x in combination) {
if (combination[x] < 50) {
var min = Math.min(combination[x])
}
}
console.log(min) //output-- 5 //it should be 2
in this third example i add (-) to 2
var combination = [27, 30, 40, 44, 3, 239, 329, -2, 5, 20923, 96]
for (let x in combination) {
if (combination[x] < 50) {
var min = Math.min(combination[x])
}
}
console.log(min) // output-- still 5 // it should be -2
again when am adding (-) to other numbers like in -96 or -5 the output was okay (-96) but when im adding (-) to 2 it is not showing me -2 in the output instead it showing me 5
not only in javascript i tried this with lua, php but output was same as js
can anyone explain me why this happen and how solve this
You're not comparing values to determine the minimum, but instead just replacing the min variable with the last number in the array that is smaller than 50. This can be fixed as follows:
let min = undefined;
for (let x in combination) {
if (combination[x] < 50) {
min = min == undefined ? combination[x] : Math.min(min, combination[x])
}
}
Using filter and reduce, this can be made a lot shorter:
combination.filter(x => x < 50).reduce((x, y) => Math.min(x, y))

How to get formulas from a row and spread them in a range using script?

How can I get this one to populate the range defined in the code?
I understand that once I get it, I have to iterate over the range to set the formulas, but I just can't get how to do it:
let formulas = boqPipeworkSheet.getRange(7, 1, 1, 8).getFormulasR1C1();
let rngFormulas = boqPipeworkSheet.getRange(7, 8, boqPipeworkSheet.getLastRow(), 8)
rngFormulas.forEach(c => rngFormulas(c).setFormulas(formulas))
Of course, it gives me an error as there's a lot to learn on forEach and a lot more.
Appreciate your time.
Got rather lost in your coordinates. Please put the ones you need.
notation: (row from num 1, column from num 1)
What this does is it copies formulas from cell (7, 1) to (7, 8).
Then it puts them on each row from row 8 till the last row. First row is (8, 1) to (8, 8).
const formulas = boqPipeworkSheet.getRange(7, 1, 1, 8).getFormulasR1C1();
const lastRow = boqPipeworkSheet.getLastRow();
for (let i = 8; i <= lastRow; i++) {
const rngFormulas = boqPipeworkSheet.getRange(i, 1, 1, 8)
rngFormulas.setFormulasR1C1(formulas);
}
Ask if you need more clarification!
In your situation, how about the following modification?
Modified script:
var src = boqPipeworkSheet.getRange(7, 1, 1, 8);
var dst = boqPipeworkSheet.getRange(7, 8, boqPipeworkSheet.getLastRow(), 8);
src.copyTo(dst, SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
Note:
If you want to copy the same formulas of boqPipeworkSheet.getRange(7, 1, 1, 8) with the same range, you can use the following modification.
var src = boqPipeworkSheet.getRange(7, 1, 1, 8);
var dst = boqPipeworkSheet.getRange(7, 8, boqPipeworkSheet.getLastRow(), 8);
dst.setFormulas(Array(dst.getNumRows()).fill(src.getFormulas()[0]));
Reference:
copyTo(destination, copyPasteType, transposed)
Added:
When I saw your script, from let rngFormulas = boqPipeworkSheet.getRange(7, 8, boqPipeworkSheet.getLastRow(), 8), I had thought that you wanted to copy the formula of A7:G7 to the H8:O. But if you want to copy the formula of A7:G7 to the A8:G, you can use the following modification. As a sample script, I would like to add this because I thought that this method might be useful for other users.
var src = boqPipeworkSheet.getRange(7, 1, 1, 8);
var dst = boqPipeworkSheet.getRange(8, 1, boqPipeworkSheet.getLastRow() - 7, 8);
src.copyTo(dst, SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);

How can I check if a number is already in an array? [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
This is the array:
array = [1, 6, 9, 10 , 15, 18, 20];
And a random number is being generated from 1 to 20, with the following code:
var x = Math.floor((Math.random() * 20) + 1)
How could I check if the generated number is already in the array above? If it is, I want to generate a new value so it does not repeat one of the values in the array.
You can use array.includes(x)
var number;
do
{
number = Math.floor((Math.random() * 20) + 1) )
} while (array.includes(number))
Just be aware of unending loop, if you fill the array.
You can use Set Api for this too
The Set object lets you store unique values of any type, whether primitive values or object references.
read more here
in your case you can do something like this
let numbers = new Set(); // your collection
const generateNumber = () => Math.floor((Math.random() * 20) + 1)
// add new number to the set
function addNumber(value) {
numbers.add(value)
}
// generate random numbers
addNumber(generateNumber())
addNumber(generateNumber())
addNumber(generateNumber())
You can use indexOf() to check if the value is in array
and after that you can use comparisson to generate new number if the random number is the same as the previous
array = [1, 6, 9, 10 , 15, 18, 20]
var x = Math.floor((Math.random() * 20) + 1) )
if (array.indexOf(x) !== -1) {
//value exists in array
var x_old=x;
x = Math.floor((Math.random() * 20) + 1) )
while (x_old === x) {
x = Math.floor((Math.random() * 20) + 1) )
}
} else {
//Value doesn't exist in array
}
Check whether the number is already in the array, if so generate a new one.
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const generateRandomNumber = () => Math.floor((Math.random() * 20) + 1)
let temp = generateRandomNumber();
// console.log(temp)
while (a.includes(temp))
temp = generateRandomNumber()
a.push(temp);
console.log(a)
do {
var x = Math.floor((Math.random() * 20) + 1);
} while (array.indexOf(x) === -1)
This code should do it, but it will compute unneccesary things. So I prefer to choose just a random item from the array like this.
var x = array[Math.floor(Math.random() * array.length)];
A lot of people are talking about the include function. I don't prefer to use that because it is a modern function that is very lately supported.
You can also do this using the below recursive function. The below solution never logs the generated random number which is present in the array, instead it generates random number recursively until, the number not in the array is found!
var array1 = [1, 6, 9, 10 , 15, 18, 20];
var x = Math.floor((Math.random() * 20) + 1);
var xIncludesArray1 = array1.includes(x);
function arrFinder() {
if(!xIncludesArray1) { console.log(x)}
else { x = Math.floor((Math.random() * 20) + 1);
xIncludesArray1 = array1.includes(x);
arrFinder();}
};
arrFinder();

Adding object properties in JavaScript

I am trying to subtract the value of object1's objectProperty from object2's object property and I keep getting NaN on the console. Here is example code:
object1.objectProperty - object2.object2Property
If this isn't enough to go off, I can post the full code from my project.
If there is another way to do this or some kind of function that can help, please let me know.
edit: Here is the code..
var myPokemon = {
health: 25,
defense: 5,
attack: 10,
speed: 5
};
var moves = {
Scratch: 5,
Bite: 5,
Slap: 5,
Growl: 1
};
var computerPokemon = {
health: 20,
defense: 5,
attack: 10,
speed: 7
};
function calcDamage(firstPokemon, secondPokemon, move) {
if(move == moves.Growl){
//starts here
var newDefense = moves.Growl - firstPokemon.defense;
console.log(newDefense);
//ends here
}else{
var newHealth = (firstPokemon.health + firstPokemon.defense) - (secondPokemon.attack + move);
console.log(newHealth);
}
}
edit: When I did
moves.Growl - firstPokemon.defense || 0; it returned -4 instead of NaN which is what I wanted it to do, but the person that answered that removed the answer so this has been answered by whoever that guy was.
The problem is that you are adding the object in the second argument. Also your if statement will never execute, I have fixed both as following
var myPokemon = {
health: 25,
defense: 5,
attack: 10,
speed: 5
};
var moves = {
Scratch: 5,
Bite: 5,
Slap: 5,
Growl: 1
};
var computerPokemon = {
health: 20,
defense: 5,
attack: 10,
speed: 7
};
function calcDamage(firstPokemon, secondPokemon, move) {
if(moves.Growl!=undefined){
//starts here
var newDefense = moves.Growl - firstPokemon.defense;
alert(newDefense);
//ends here
}else{
var newHealth = (Number(firstPokemon.health) + Number(firstPokemon.defense)) - (Number(secondPokemon.attack) + Number(move.Growl));
alert(newHealth);
}
}
calcDamage(myPokemon,computerPokemon,moves)
Usually, if you are getting NaN, you are probably working with other elements but numbers. Are you sure they both are numbers?
Just an example:
var x = {}, y = {};
x.r = 10;
y.r = 5;
x.r - y.r; // yields 5
Use parseInt to convert the values into integer and then do the math.
var value = parseInt(object1.objectProperty,10) - parseInt(object2.object2Property,10);
The Problem is here
var newHealth = (Number(firstPokemon.health) + Number(firstPokemon.defense)) - (Number(secondPokemon.attack) + Number(move.Growl));
The last one Number(move.Growl) it should be Number(moves.Growl) moves not move. In your case move is just a number and you are trying Number(move.Growl) which will be NaN and hence your getting NaN.

Using _.countBy with ranges

I have a list of FICO scores that I'd like to group depending on their value, with groups starting at 0-620, then 620-640, incrementing by 20 every time.
What it should return is a count of values in each group -- 0-620, 620-640, 640-660, 660-680, etc. up to 780-800.
I'm currently using _.countBy but it returns a count for every unique number, rather than for every group.
var counts = _.countBy(ficos, function(x) { return x });
//Counts should be {620: 22, 625: 19, 655: 24, 670: 20, 690: 30, 720: 29, 734: 17, 760: 21, 790: 18}
Is there a way to take advantage of _.countBy or some other function? I'm trying to avoid a long if statement, if possible.
So return the appropriate groups then:
function (x) {
if (x < 620) return '<620';
if (x >= 800) return '>800';
var lower = Math.floor(x / 20) * 20,
higher = lower + 20;
return lower + '-' + higher;
}

Categories

Resources