EvilZone
Programming and Scripting => Web Oriented Coding => Topic started by: nizzel 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:
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
-
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.
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.
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.
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 " ` " .