I was aiming to make this problem shorter with less variables and to only mutate the original array1, but after I noticed that addedOddNumbers (value of 9) was not adding to the odd numbers of array1 while using a for loop I decided to break it down as much as possible.
When I started to break it down as shown below I then tried adding addedEvenNumbers to over10 with the for loop. This worked and gave me 23, 21, but the same if statement above with addedOddNumbers to under10 prints me 2, 4, 9.
Meanwhile addedEvenNumbers prints out 23 and 21 which was what I was looking for originally with the addedOddNumbers too. Can someone please explain why that although they are the same exact if statement/expression that one is iterating through and adding while the other is not?
Note: I'd like to see this solved as close as possible to to what I have written out. I have seen other solutions, but I can't read the other solutions to be able to answer my own question.
function addingAllTheWeirdStuff(array1, array2) {
let addedOddNumbers = 0 // 9
let addedEvenNumbers = 0 // 6
let under10 = [] // 1, 3, 5
let over10 = [] // 17, 15
let result = [] // should be 10, 12, 14, 23, 21 --- current result is 2, 4, 9, 23, 21
for (let i = 0; i < array2.length; i++) {
if (array2[i] % 2 === 1) {
addedOddNumbers += array2[i]
}
if (array2[i] % 2 === 0) {
addedEvenNumbers += array2[i]
}
if (array1[i] < 10) {
under10.push(array1[i] + addedOddNumbers)
}
if (array1[i] > 10) {
over10.push(array1[i] + addedEvenNumbers)
}
}
console.log(addedOddNumbers)
console.log(addedEvenNumbers)
console.log(under10) // why????
console.log(over10) // this works fine but why not under10?
result.push(...under10, ...over10)
console.log(result)
}
console.log(addingAllTheWeirdStuff([1, 3, 5, 17, 15], [1, 2, 3, 4, 5]));
// expected log [10, 12, 14, 23, 21]
It's not the final result of addedOddNumbers and addedEvenNumbers that is added to the values of over10 and under10, but their current value inside the loop. The over10 values are correct because they are at the end of the array and by then all the even numbers are already added to addedEvenNumbers.
Related
My task is like this:
You will see the array 'numbers'. Use a for loop to go through that array. For each element in the array, print the number to the console using console.log. If the number is divisible by 3, instead of the number print the string 'apple' to the console. Should the number be divisible by 5, print the string 'cake' to the console instead of the number.
In the first task, I used a for loop to show every number from 1-20, so I can't seem to understand why I can't get this to work. I am maybe thinking I have to implement a new for loop in this task too.
My code is like this:
console.log('PART 3')
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
if (numbers % 3){
console.log('eple');
}
else if(numbers % 5){
console.log('kake');
}
You cannot use modulo (Reminder Operator %) on an array.
You're missing a for loop (as stated by the task requirement) in order to iterate over its values:
console.log('PART 3')
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
for (let number of numbers) {
if (number % 3){
console.log(number + ' eple');
}
else if(number % 5){
console.log(number + ' kake');
}
}
In order to find out if a number is a factor of another number:
number % 3 === 0
That translates as: If a number divided by 3 has a remainder of 0...
You don't even have a for loop
for (let i=0; i < array.length; i++) {...}
There are 3 expressions of a for loop statement
Expression
Example
Description
Initial
let i=0
Define variable at the beginning
Condition
i < array.length
When variable no longer less than the quantity of elements within the array stop loop
Increment
i++
Variable increments by 1
Flow control statements can be only in a certain order:
if (a > b) {...}
// OR
if (a > b) {...}
else {...}
// OR
if (a > b) {...}
else if (a === b) {...}
// ...more else if(...){...} if required
else {...}
Never do this
if (a > b) {...}
else if (a === b) {...}
// This will create an array of 1 - 20 programatically
const array = [...Array(20)].map((_, i) => i + 1);
console.log(JSON.stringify(array));
for (let i = 0; i < array.length; i++) {
let number = array[i];
if (number % 3 === 0) {
console.log("APPLE");
} else if (number % 5 === 0) {
console.log("CAKE");
} else {
console.log(number);
}
}
console.log('PART 3')
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
const output = [];
for(i=numbers[0];i<=numbers.length;i++){
if(numbers[i]%3==0 && numbers[i]%5==0){
output.push(numbers[i] + " :Apple, Cake")
console.log(output);
}
else if(numbers[i]%5==0){
output.push(numbers[i] + " :Cake")
console.log(output);
}
else if(numbers[i]%3==0){
output.push(numbers[i] + " :Apple")
console.log(output);
}
}
I have an array with some numbers like the following:
[1, 2, 3, 4, 6, 7, 8, 10, 15, 16, 17]
I'd like to show all numbers that are direct after each other (n+1) in one line and if there is a gap, this should be separated. This will either be done in javascript/jquery.
The user would see it like this:
1 - 4, 6 - 8, 10, 15 - 17
I'm guessing the only solution to this would be to loop through the array and see if the next number is n+1 and if it is, lump it together, else start on a new series?
I think I know how I would do it that way but interested to know if there is some other way to do it either in javascript/jquery?
You can loop once while keeping track of the current starting number.
let arr = [1, 2, 3, 4, 6, 7, 8, 10, 15, 16, 17];
let start = arr[0],
res = [];
for (let i = 1; i < arr.length; i++) {
if (arr[i + 1] - arr[i] != 1 || i == arr.length - 1) {
res.push(start + " - " + arr[i]);
start = arr[i + 1];
}
}
console.log(res);
I'm currently working on an array function that converts subarrays of consecutive numbers into strings denoting that range of numbers — for example, this array...
[1, 2, 3, 6, 8, 10, 11, 12, 15, 18]
...would become this array:
["1-3", 6, 8, "10-12", 15, 18]
I've been able to develop a function that mostly works, but I've encountered a weird error where all the elements past the final range of numbers spliced into the array are completely deleted. For example, the test array above actually becomes this:
["1-3", 6, 8, "10-12"]
This is the code I've written so far. It's not super pretty yet, but as I mentioned above, it gets the job done right up until the very end:
let testArray = [1, 2, 3, 6, 8, 10, 11, 12, 15, 18];
for (i = 0; i < testArray.length; i++) {
let consecutives = [];
consecutives.push(testArray[i]);
let j = i + 1;
while (j < testArray.length) {
if (testArray[j] == (testArray[j - 1] + 1)) {
consecutives.push(testArray[j]);
j++;
} else {
break;
}
}
if (consecutives.length > 2) {
let range = String(testArray[i]) + "-" + String(testArray[j - 1]);
console.log(testArray);
console.log(testArray[i]);
console.log(testArray[j]);
testArray.splice(i, j, range);
}
}
console.log(testArray);
These are the console logs output by that code:
Array(10) [ 1, 2, 3, 6, 8, 10, 11, 12, 15, 18 ]
1
6
Array(8) [ "1-3", 6, 8, 10, 11, 12, 15, 18 ]
10
15
Array(4) [ "1-3", 6, 8, "10-12" ]
I initially figured this was caused by a mix-up with array indexes, but playing around with the index-1s hasn't fixed the problem yet. Has anyone else ever had a similar issue with JavaScript's splicing, and if so, how were you able to get it working?
The problem lies in one line of code:
testArray.splice(i, j, range);
According to the MDN, the second argument specifies how many elements in the array to delete.
deleteCount
An integer indicating the number of elements in the array to remove from start.
However, the code defines this argument as the index of the last array to remove from:
let j = i + 1;
The solution is to get the difference between i and j before passing it to splice:
testArray.splice(i, j - i, range);
When you do:
testArray.splice(i, j, range);
You are forgetting that j is the right limit index of the array that you want to erase, so you need to subtract i that is the left limit:
testArray.splice(i, j - i, range);
let testArray = [1, 2, 3, 6, 8, 10, 11, 12, 15, 18];
for (i = 0; i < testArray.length; i++) {
let consecutives = [];
consecutives.push(testArray[i]);
let j = i + 1;
while (j < testArray.length) {
if (testArray[j] == (testArray[j - 1] + 1)) {
consecutives.push(testArray[j]);
j++;
} else {
break;
}
}
if (consecutives.length > 2) { // doesn´t it should be > 1 ??
let range = String(testArray[i]) + "-" + String(testArray[j - 1]);
console.log(testArray);
console.log(testArray[i]);
console.log(testArray[j]);
testArray.splice(i, j - i, range);
}
}
console.log(testArray);
I can't get a result I wanted:
Even Array is: 2, 18, 38, -10, 0, 104
Odd Array is: 3, 13, -5, 11
Two arrays where one contains even numbers another odd.
Here is the code:
let arrayMain = [2, 3, 13, 18, -5, 38, -10, 11, 0, 104];
let oddArray = [];
let evenArray = [];
for (let i = 0; i < arrayMain.length; i++){
if (i % 2 == 0) {
evenArray.push(arrayMain[i]);
}
else {
oddArray.push(arrayMain[i]);
}
}
console.log("Odd Array is " + oddArray);
console.log("Even Array is " + evenArray);
It gives me:
Odd Array is 3,18,38,11,104
Even Array is 2,13,-5,-10,0
How can I fix this?
You need to check the value, not the index.
if (arrayMain[i] % 2 == 0) {
// ^^^^^^^^^^ ^
suppose we have this two array. in some condition, I want to return the index of a second array.
let a = [1, 2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 , 12]
let b = [0, 1, 2, 3 , 4 , 5, 6 , 7 , 8, 9, 10, 11]
if (a[2]) ? return b[2] : return null
why I need this? because I have a month number of the year that starts from 0 to 11. but I need to turn this 1 12 for storing in my database.
sry about the title of this question if someone has a better title I would be glad about changing it.
You could calculate the value by adding 11 and get the remainder value with 12.
function getZeroBasedMonth(n) {
return (n + 11) % 12;
}
console.log(getZeroBasedMonth(1));
console.log(getZeroBasedMonth(12));
For the getting the opposite, just add one.
function getMonth(n) {
return n + 1;
}
console.log(getMonth(0));
console.log(getMonth(11));
Why make this harder than it needs to be? Just add 1 to the value you get from your initial array. Here is, per your comment, 10 years worth of values for the months with a +1 value.
let years = 10;
let months = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
let allYears = [];
for(let i = 0; i < years; i++){
let year = [];
for(let x = 0; x < months.length; x++){
year[x] = months[x] + 1;
}
allYears.push(year);
}
console.log(allYears);