My code only gives back numbers instead of letters


#1



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?


https://www.codecademy.com/courses/javascript-beginner-en-XEDZA/0/5?curriculum_id=506324b3a7dffd00020bf661

text = "blah blah blah blah blah Chris blah\
blah blah blah blah mike blah 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++){
hits.push(text[j]);
}
}
}
if (hits.length === 0) {
console.log("Your name wasn't found");
}
else
{
console.log(hits);
}

Replace this line with your code.


#2

@gompofa,
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\
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
    hits.push(text[j]);
}
if (hits.length === 0) {
     console.log("Your name wasn't found");
}
else {
 console.log(hits);
}

#3

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


#4

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.


#5

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


#6

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.


#7

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

https://jsbin.com/yuralo/edit?js,console

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


#8

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.


#9

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


#10

@puristaja,
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...!!
http://javascript.crockford.com/survey.html ( Crockford as most respected propagator )
Also read through the code-convention's http://javascript.crockford.com/code.html

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:

•Number
•String
•Boolean
•Symbol
•Object  ◦Function/'Method'
         â—¦Array
         â—¦Date
         â—¦RegExp
         â—¦Math
•Null
•Undefined

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 site:developer.mozilla.org
and choose
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
on the left hand side you will see
what information you can gather about the array

PropertiesArray.prototype

Array.prototype.length

  • 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 )

Array.isArray()
Array.prototype.concat()
Array.prototype.every()
Array.prototype.filter()
Array.prototype.forEach()
Array.prototype.indexOf()
Array.prototype.join()
Array.prototype.lastIndexOf()
Array.prototype.map()
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.slice()
Array.prototype.some()
Array.prototype.sort()
Array.prototype.splice()
Array.prototype.toLocaleString()
Array.prototype.toString()
Array.prototype.unshift()

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

The most common way to loop over all elements of an Array
is using the generic FOR-loop
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

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

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 site:stackoverflow.com
for instance
= http://stackoverflow.com/questions/5269757/why-is-javascripts-for-in-loop-not-recommended-for-arrays
[quote]
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]

==================================================================

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html <---JavaScript code-convention
http://javascript.crockford.com/survey.html

https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide

http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript

http://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function

http://stackoverflow.com/questions/6260475/how-to-the-operator

http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname?rq=1


#11

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