☜ Back

Exercises in Arrays continued

Add things to elements in an array that meet a test or condition(s)

Next I am going to try and add a flag to each element in an array within a range of values in this case greater than what is specified by the user:

This is a nice little touch this is a tick using Unicode character \ ☠ \are well can't type it because it just shows up as a tick.This can be used to tick results using javascript ✔ ✔ ✔ i think I might be going a bit tick crazy, you can see what might be coming?

Original
Above what?

The script for the ticking exercise. It is really quite clever as the first array is overwritten by the new array making it look as if the values have been ticked. To do this you need to declare the empty array that is going to contain the new array outside the function and create the new array inside the function. I have improve this further by adding an input box where you can select what number to use as the test.

Lets just store something

Behind this is some code it saves the word "Streeter" as a literal string to local memory as part of a key, value pair. In this case the key was "lastname"

Next I want to be able to store user input. I will expand these notes bear with me.

Input a word to save:

The word is displayed above, it has been saved to local memory as a variable! not a string (although the word itself is a text string) it is the value of a key/value pair.

Next store an array

I want to be able to store an array as the value in a key/value pair in local memory so I can go back to it and add to it later.

This was the stored array.

Now the difficult bit add an array to it

So lets use the array I have already created called 'firstTot' and stored as 'Dtot' then add a second called 'secTot. The first problem we encounter is that localStorage stores things as strings. So when we retrieve it and turn it into a new temporary value we need to convert it back to an array using the string split method.