Author Topic: Need help with this piece of javascript code  (Read 786 times)

0 Members and 1 Guest are viewing this topic.

Offline nizzel

  • /dev/null
  • *
  • Posts: 5
  • Cookies: -4
    • View Profile
Need help with this piece of javascript code
« on: September 20, 2015, 08:25:47 pm »
So, i m learning javascript and i got this problem:

I have to print all the phrases but not the  numbers in this object:
Code: (javascript) [Select]
var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!" };
------------------------------------------My piece of code to solve this problem was---------------------------
for( var xix in languages) {
   
    var nizzel = false
   
    for (var x = 0; x <= languages[xix].length; x++) {
        if(languages[xix[0]]= " ยด " )
       nizzel = true
     else{ nizzel = false}}
     
     
     if( nizzel = true) { console.log( languages[xix] + nizzel)}
     
     
     }
     
     -------------------------------------------------------------------------------------------------

The problem is that it prints everything.... and i cant understand why....
I know i can do this using typeof === "string" and whatever... but i wanted to try this way!


Can you help me fixing the code and not changing it completely?

Staff edit: Please use code tags when posting code. Thank you
« Last Edit: September 20, 2015, 08:39:46 pm by iTpHo3NiX »

Offline gray-fox

  • Knight
  • **
  • Posts: 208
  • Cookies: 52
    • View Profile
Re: Need help with this piece of javascript code
« Reply #1 on: September 20, 2015, 10:34:07 pm »
Javascript ain't really my thing and have to say I have no idea what is meaning of " ` " . I see what you are trying to do with that if statement, but that syntax doesnt make any sense to me.

But reason why it prints all.
Code: [Select]
if( nizzel = true) {console.log( languages[xix] + nizzel);}
You are not testing if nizzel is true instead you're allways assigning it to be true.

But anyways I still made my own example which I think will work and might give you some ideas what to do and what errors you made. You tried to make things bit too complicated imo, maybe you see what I mean from my example.
Code: [Select]
var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!" };
   
for ( var xix in languages) {
    if ( /^([^0-9]*)$/.test(languages[xix]) ){
        console.log( languages[xix]);
    }
}

I know you asked not to change code so much, but like I said this didn't really make any sense to me.
Code: [Select]
if(languages[xix[0]]= " ´ " )
Once again you're not comparing anything, instead you're assigning value by using "=".

Edit: I modified  some parts of my post after I took another look at your code.  Still don't quit get how that if statement is suppose to work anhways with that " ` " .
« Last Edit: September 20, 2015, 11:40:23 pm by gray-fox »