/    Sign up×
Community /Pin to ProfileBookmark

JavaScript and strings

In javascript, i know it is possible to use and manipulate strings. I looked into the string object, but it doesen’t have just plain ol’ text, always bold, italic, etc. So, i need to use somthing like goes into a document.write, a string in quotes, that i can access at any time.

I have a marquee that i am working on, using the DOM, i am scrolling it in a 1-row table. Each cell in the single row is identified by a base followed by a number (so that more than one of these could be on a page).

The function scroll(mx, base, id, msg) is used to put the message into the table.

mx – the maximum cell number for the table, cells starting at 1
base – the base name for the cells
id – the number for an array containing the starting character
msg – the string representation of the message to scroll

the function goes like this:

var chararray = new Array();

[code]
function scroll(mx, base, id, msg)
{
var x = 0;
var c = 0;

//increment the current character
chararray[id]++;

//put it into a variable we can change a bunch
c = chararray[id];

//loop once for each cell
for(x = 1; x < mx; x++)
{
//test if the current character number is too big; fix the problem
if c > msg.length
c = 0;
//put the character into the current cell
document.getElementById(base + x).firstChild.data = msg[c];
}
}
[/code]

Now, this should, in theory, fill a table like this:
[ ][ ][ ][ ][ ][ ][ ][ ][ ][1]
[ ][ ][ ][ ][ ][ ][ ][ ][1][2]
[ ][ ][ ][ ][ ][ ][ ][1][2][3]
[ ][ ][ ][ ][ ][ ][1][2][3][ ]
as long as 2 things are true:
1. chararray MUST be persistent between calls to the function
2. in the last statement, (base + x) comes out as a string

so, is there a way to make chararray persistent between calls?
and, does (base + x) come out as a string?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@jakykongauthorFeb 22.2005 — Ok, let me simplify my question:

Is JavaScript a typed language? (do i need a special function to convert x, which is a number, into a string before i concatenate it to the end of base, which is already a string?)

If JavaScript is a typed language, what is the function to convert a number to a string?

How do i make the values of an array not be erased when the function is done doing it's thing, so that the next time the function is called, the state is saved?
Copy linkTweet thisAlerts:
@JuuitchanFeb 23.2005 — This shows how JavaScript works:
<i>
</i> 2 + 3 evaluates to 5
"2"+ 3 evaluates to "23"
"2"+"3" evaluates to "23"
2 - 3 evaluates to -1
"2"-"3" evaluates to -1
2 + "" evaluates to "2"
"2"- 0 evaluates to 2

Am I missing anything?
Copy linkTweet thisAlerts:
@jakykongauthorFeb 23.2005 — how about division? no, jk ... i think you got about everything there, and, although it didn't solve the problem, it was a major help!


...

one thing is still missing, i need a variable that stays alive as long as the client is on the page ... so that i can keep track of what needs to go where.
Copy linkTweet thisAlerts:
@JPnycFeb 23.2005 — No, javascript is not a strongly typed language. Most of the scripting languages aren't. Javascript will do auto-type conversions when you concatenate, but it's best to cast them yourself, to avoid unpredictable results.
Copy linkTweet thisAlerts:
@strBeanJul 30.2005 — 

I'm definitely no expert, but I just read somewhere that if you declare a variable outside any function, it stays alive as long as the document is open...

By the way:

Sorry to hijack your thread , but have you come across a way to concatenate a carriage return line feed into a string?
Copy linkTweet thisAlerts:
@strBeanJul 30.2005 — I just tested the scope and life of a variable declared outside any function, but within the <script> tags. It works.

And BTW, I got the answer to the question I chimed in with.

thx
×

Success!

Help @jakykong 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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