My code only gives back numbers instead of letters


I have tried editing this code over and over again but I can not find what is wrong with it.

I have looked at other people's examples but can not see what is wrong. Can someone help?

text = "blah blah blah blah blah Chris blah\
blah blah blah blah mike blah blah blah blah\
var myName= "Chris";
var hits = [];

for (var i = 0;i < text.length; i++);{
if (text[i] === "C");{
for (var j = i; j < (myName.length + i); j++){
if (hits.length === 0) {
console.log("Your name wasn't found");

Replace this line with your code.


For the Javascript Interpreter a semi-colon-; is an End-of-Statement indicator
causing the code, as presented in your Original Post
to be interpreted as

text = "blah blah blah blah blah Chris blah\
blah blah blah blah mike blah blah blah blah\
var myName= "Chris";
var hits = [];

for (var i = 0;i < text.length; i++);
// variable =i= now has the VALUE text.length
// the IF statement is executed, 
// the outcome has no effect on the flow 
if (text[i] === "C");
// in the FOR-loop =j= now gets the VALUE of =i= which has the VALUE text.length
for (var j = i; j < (myName.length + i); j++){
    // you now try to collect characters outside of the text.....!!! 
    // you get =undefined= as return
    // ( 5x ) Chris.length
if (hits.length === 0) {
     console.log("Your name wasn't found");
else {


Just wondering, why check hits.length instead of hits in the if/else statement?


I'm guessing because he has already defined hits as an array

var hits = [];

if we would simply ask if(hits){} it would return true always, even if it is an empty array.


You are right. What I meant to write is why do we check (hits.length === 0) instead of if (hits === 0)?


if you use === the x and y must be equal in value and equal type, as hits is an array and 0 an int (also null? @leonhard.wettengmx.n, help me out).... I don't have time to test but I guess the interpreter would throw some kind of an error.


I gave it a shot here with a modified string for the text variable. Here, (hits === 0) is the condition.,console

No error related to if (hits ===0).


I'm confused by this. Exactly when should a ; be used?

It seems to have a large dictation as to whether the code will work or not.


I've been thinking. Is it almost used as if it was a '.' at the end of a sentence?


First of all,
Always try to name the beast properly....

It is correct, that if you use the strict equal operator === in your comparison
Returns true if the operands are equal and of the same type

Javascript is an Object Oriented, classless but prototypal programming language...!! ( Crockford as most respected propagator )
Also read through the code-convention's

JavaScript contains a small set of data types.

  • It has the three primitive types
    • boolean,
    • number,
    • and string
  • and the special value null
  • and the special value undefined.
  • Everything else is variations on the object type.

An object has one or more properties, separated by a comma-,

A property has a property-key and it's associated VALUE

The associated VALUE can be anything from:

•Object  ◦Function/'Method'

Above you have an overview of all data-types.

===== Datatype Array =========
What you must understand,
is that if you ( as an example )
create a array
for the Javascript-Interpreter
you have created an object of the prototype array.
and if you do a google search
javascript array
and choose
on the left hand side you will see
what information you can gather about the array



  • The value of the length property is an integer with a positive sign and a value less than 2 to the 32nd power (232).
    Thus a strict-equal === comparison of Array.length with zero could lead to a Boolean true if the the Array is-empty, has no-Elements

Methods ( pre-defined functions for Array operations )


========= looping over an array ===

The most common way to loop over all elements of an Array
is using the generic FOR-loop

for ([initialization]; [condition]; [final-expression]) {

You would
start at index zero,
continue as long as the index is =less than= the array.length,
and at each iteration increment / decrement the index-pointer

var myArray =["a",2,{anObjectKey: "anObjectValue"}];
for ( var index = 0, len = myArray.length; index < len ; index= index + 1 ) {
     // get access to the =element-Value= using the [bracket-notation]
     console.log( index + "==> "+myArray[index] );

== Do NOT use FOR-IN loop on an Array ==

You could look at an Array as it being an Object like

 myArray = {
      0: "a",
      1: 2,
      2: { anObjectKey: "anObjectValue"}

and with the FOR-IN loop

for (var key in myArray) {
     //getting access to the =associate property-Value= 
      //using the =key= in a [bracket-notation]
      console.log( key + "==>"+myArray[key] );

but do a google search
== discussions / opinions ==
javascript why is it a bad idea to use for-in on an Array
for instance
An array is an object,
and array elements are simply properties with the numeric index converted to string.
For example, arr[123] refers to a property "123" in the array object arr.

The for ... in construct
works on all objects, not just arrays,
and that is cause for confusion.

When somebody for ... in an array,
most often the programmer intends to iterate just all the items, even most likely in order.

The semantics is so similar to array iteration in other programming languages that it is very easy to get confused.
In JavaScript, this construct does not iterate array elements in order.
It iterates all

  • the array's properties
  • (including inherited prototype functions, any properties added to it, any other non-element properties added to it etc.),
  • and not in order at all.
  • It will even find the property "length".

This is most likely not what the programmer means, if he/she comes from another programming language.

That's why you shouldn't do it.
You shouldn't use a language construct
that looks like it does obvious things, but actually does things that are completely different.
It creates bugs that are very obscure and very difficult to find.
[end quote Stephen Chung]



google search
== the Book ==
javascript [your question]

== discussions / opinions ==
javascript [your question]

== guidance == <---JavaScript code-convention


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.