/    Sign up×
Community /Pin to ProfileBookmark

(beginner) split and process lists

Hey wats up, im trying to split a list,using the first half process with += -=, and the second half process with the first half += -=? eg 0,1,2,10,10,10 the final list would be 22,10,-2.

i can generate the values but how would i add them? could i declare them as variables or something? im only 2 wks in so any advice is appreciated.

[code] a = [0,1,2,1,10,10,10,10,10]

a.forEach(function(item, i) {
if (item == 0) a[i] += 12;
if (item == 1) a[i] += 0;
if (item == 2) a[i] -= -12;
});[/code]

Thanks

Remark by Sempervivum: Unfortunately the forum software had eaten your [i]. You better use code tags when posting code: [code]your code here[/code]
I edited this posting accordingly.

to post a comment
JavaScript

16 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJun 16.2020 — Can you state the nature of your problem with some other words?

I am unclear of the expected results and how they are supposed to be generated.
Copy linkTweet thisAlerts:
@xXBrodieXxauthorJun 16.2020 — Thanks JMRKER i did seperate the array now id like to call +=12, +=0 or -=12 on each element depending on the index value of the other array eg. if (0) add12to10; if (1) add0to10; if (2) minus12from10; theres proly a better solution but im still basic.
Copy linkTweet thisAlerts:
@xXBrodieXxauthorJun 16.2020 — i decalared >>

var x = function(){x += 12;}

var y = function(){y += 0;}

var z = function(){z -= 12;}

but im not sure how i can call a function on one array from another.Any suggestions are aprreciated.
Copy linkTweet thisAlerts:
@daveyerwinJun 16.2020 — "Hey wats up, im trying to split a list"

We don't know what you mean by that ?

Maybe you want to make two arrays

from the array [0,1,2,1,10,10,10,10,10] ?

Anyways ...

``<i>
</i>a = [0,1,2,1,10,10,10,10,10]

a.forEach(function(item, index) {
console.log(item, index);
});<i>
</i>
``


run that in your browser then press F12

it will show this ...

0 0

1 1

2 2

1 3

10 4

10 5

10 6

10 7

10 8

hopefully, that will give you an idea about how forEach works

keep asking questions

we will try to guide you
Copy linkTweet thisAlerts:
@xXBrodieXxauthorJun 16.2020 — Thanks my biggest problem was trying to impose some math from one array to another? not really sure how to format it tbh i need the state of array1 to declare what formular will be executed?
Copy linkTweet thisAlerts:
@daveyerwinJun 16.2020 — @xXBrodieXx#1619585

" i need the state of array1 to declare what formular will be executed?"

i am hearing this

maybe Im wrong ...

you want to use the value of index 0 of array "a"

a = [0,1,2,1]

to determine which function will be called with

the value of index 0 of array "b" as the argument

b = [10,10,10,10]

(the argument is the value passed to the function)

maybe this will be more clear

let me change the example to this ...
``<i>
</i>a = [0,1,2,1,10,10,10,10,10]

a.forEach(function(value, index) {
console.log(value, index);
});<i>
</i>
``


run that in your browser then press F12

it will show this ...

0 0

1 1

2 2

1 3

10 4

10 5

10 6

10 7

10 8

hang in there

when we can talk to each other with the same terms

(index value argument function)

i will give you the complete code
Copy linkTweet thisAlerts:
@xXBrodieXxauthorJun 16.2020 — Ill try explain..

Array 1 is made up of 0s 1s and 2s and Array 2 is random values from 0 to 127. Array 1 values eg -12, 0,+12 need to be added to each element of array 2, not to replace them.

My objective- If (arr1[0]=0){arr2[0]+12)} If (arr1[0]=1){arr2[0]+0)} If (arr1[0]=2){arr2[0]-12)} but i think i need variables.the array will be from 3 to 25 elements..the list is one big array but they are coming from outside js as 2 lists that are combined as one,
Copy linkTweet thisAlerts:
@daveyerwinJun 16.2020 — <script>

arr1 = [0,1,2,0];

arr2 = [2,9,3,6];

arr1.forEach(function(value, index) {

if(value == 0){arr2[index] += 12};

if(value == 1){arr2[index] += 0};

if(value == 2){arr2[index] += -12};

});

alert(arr2);

</script>

arr2 now equals [14,9,-9,18]


"the list is one big array but they are coming from outside js as 2 lists that are combined as one"

say array if you mean array, don't say list unless you mean list

where is outside Javascript ?

Copy linkTweet thisAlerts:
@xXBrodieXxauthorJun 17.2020 — yess of course so straight forward, i must be over thinking.Thanks DaveyErwin i was close to..
Copy linkTweet thisAlerts:
@JMRKERJun 17.2020 — My take on the problem (based upon 'DaveyErwin's understanding example)

'

<!DOCTYPE html><html lang="en"><head><title> Test Page </title>

<meta charset="UTF-8">

<meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/>

<!-- For: https://www.webdeveloper.com/d/390310-beginner-split-and-process-lists/8 -->

</head><body>

<pre id='demo'></pre>


<script>
arr1 = [12,0,-12];
arr2 = [2,9,3,6];
arr3 = [0,1,2,3,4,5,6,7,8,9];

function specialAdds(a, b) {
b.forEach(function(value, index) {
var i = index % a.length;
b[index] += a[i];
});
return b;
}

document.getElementById('demo').innerHTML += 'arr1: '+arr1+'n';
document.getElementById('demo').innerHTML += 'arr2: '+arr2+'n';
document.getElementById('demo').innerHTML += 'arr3: '+arr3+'nn';
// document.getElementById('demo').innerHTML += specialAdds(arr1,arr2)+'nn';
// document.getElementById('demo').innerHTML += specialAdds(arr1,arr3)+'nn';

document.getElementById('demo').innerHTML += Special addition of arr1 and arr2:
<i> </i> ${specialAdds(arr1,arr2)}nn
;
document.getElementById('demo').innerHTML += Special addition of arr1 and arr3:
<i> </i> ${specialAdds(arr1,arr3)}nn
;
</script>
</body></html>

`
Copy linkTweet thisAlerts:
@xXBrodieXxauthorJun 17.2020 — Thanks JMRKER not actually using any html,im using the js script inside Max i havent attempted any html yet.

Copy linkTweet thisAlerts:
@JMRKERJun 17.2020 — HTML there ONLY to display results without using the alert.

It has no effect on the output of the JS function that performs the actions to display.
Copy linkTweet thisAlerts:
@xXBrodieXxauthorJun 17.2020 — alright thanks do i call specialAdds?
Copy linkTweet thisAlerts:
@lounaxeniaJun 17.2020 — HTML there ONLY to show results without utilizing the caution. It has no impact on the yield of the JS work that plays out the activities to appear.
Copy linkTweet thisAlerts:
@daveyerwinJun 17.2020 — @xXBrodieXx#1619623

you don't need any HTML

I just learned what MAX8 is

I will guess you are working on

some type of tremolo or vibrato

or creating motion in the visual

display ?
Copy linkTweet thisAlerts:
@JMRKERJun 19.2020 — @xXBrodieXx#1619623

No HTML needed, it was included just to see results without an alert.

<script>

arr1 = [12,0,-12];

arr2 = [2,9,3,6];

arr3 = [0,1,2,3,4,5,6,7,8,9];

function specialAdds(a, b) {

b.forEach(function(value, index) {

var i = index % a.length;

b[index] += a[i];

});

return b;

}



var arr12 = specialAdds(arr1,arr2);

var arr13 = specialAdds(arr1,arr3);



alert('arr1: '+arr1+'narr2: '+arr2+'n'+arr3);

alert('arr12: '+arr12+'narr13: '+arr13);



</script>

×

Success!

Help @xXBrodieXx spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...