Jump to content

HOLY ROBIN, BATMAN!


L8V2L

Recommended Posts

Because if you didn't initialize it as an empty string the result will start off saying "undefined" and some browsers will throw an error message "text is not defined"

  • Like 1
Link to comment
Share on other sites

it's also a good practice to initialize your variables to a default value, ideally one indicative of the data type it's going store, especially in the case of a loosely typed language like Javascript.

Link to comment
Share on other sites

I like to alter the code sometimes when I go through the tutorial; as I am going through w3s js tutorial. And reading books on js, and etc.

(1 == 0) ? (a = "yes: 1 == 0") : (1 < 0) ? (a = "yes 1 < 0") : (1 <= 0) ? (a = "1 <= 0") : (0 >= 1) ? (a = "0 >= 1") : (a = "none of thee preceder"); console.log(a);var x = undefined, y = x;function myFunction(x, y) {(x == undefined && y == undefined) ? (x = 2, y = 12) : (x == undefined) ? (x = 12) : (y == undefined) ? (y = 2) : (x = x, y = y);   return x * y;}//document.getElementById("demo").innerHTML = myFunction();console.log(myFunction());
Multiple times, I wanted to post here for an answer, but I knew if it turn out to be simple, I would feel less. So for two hours or three, or maybe two and a half I spent trying to figure this out.How to concatenate multiple ternaries together. The problem was how I was arranging the condition of the conat-ternaries.I deserve a like... a point. Please someone give me a point for this simple, non-meaningful(concept in learning) trail....It doesn't matter if I figure it out by myself, the fact of how long it took me to get it right is defeat enough to make me feel less.
Link to comment
Share on other sites

Because if you didn't initialize it as an empty string the result will start off saying "undefined" and some browsers will throw an error message "text is not defined"

Thanks Ingolme.
Link to comment
Share on other sites

why are you over complicating it? Do you even know how a simple ternary operator works? Both your examples are extremely convoluted and impractical for a real life application (and from what I can see won't even work).

 

As we keep saying, keep it simple until you understand the language and know what you're doing at a basic level first.

function checkSecretWord(guess){  var isCorrectGuess = guess === 'w3schools' ? 'Correct' : 'Incorrect guess, try again';   return isCorrectGuess;} console.log(checkSecretWord('mdn'));  //logs 'Incorrect guess, try again'console.log(checkSecretWord('microsoft'));  //logs 'Incorrect guess, try again'console.log(checkSecretWord('w3schools'));  //logs 'Correct'
Link to comment
Share on other sites

it's also a good practice to initialize your variables to a default value, ideally one indicative of the data type it's going store, especially in the case of a loosely typed language like Javascript.

question 1) So if the variable x is going to hold a string value down the line. When declare, it'll be good practice to initialize it with a empty string value <-- Is that what you are saying?question 2) do this for only primitive values?question 3) Or both primitive, and composition values? Composition; for object {}, and for array [], and even for function... maybe not for function .Please answer all with yes or no, and if other please put at bottom of the replies which should be either yes or no.... <~~~ I hope this isn't seem as demanding. I'm just making sure of clarity for my sake.
Link to comment
Share on other sites

You should initialize any variable. Sometimes you initialize it to a default value, sometimes you initialize it to the final value it's going to have, it depends what it's for. Any programming book is going to cover variable initialization, and variables are one of the first things programming books cover, so that leaves me to wonder why you don't understand that. It doesn't make sense that you're asking about something as complex as grouping multiple ternary expressions, and then you don't understand why you should initialize a variable. It sounds like you're skipping around all over the place instead of starting at the beginning and going from there.

Link to comment
Share on other sites

why are you over complicating it? Do you even know how a simple ternary operator works? Both your examples are extremely convoluted and impractical for a real life application (and from what I can see won't even work). As we keep saying, keep it simple until you understand the language and know what you're doing at a basic level first.

function checkSecretWord(guess){  var isCorrectGuess = guess === 'w3schools' ? 'Correct' : 'Incorrect guess, try again';   return isCorrectGuess;} console.log(checkSecretWord('mdn'));  //logs 'Incorrect guess, try again'console.log(checkSecretWord('microsoft'));  //logs 'Incorrect guess, try again'console.log(checkSecretWord('w3schools'));  //logs 'Correct'

 

This is for (What I want to do) thing... Please take no offense to that, what I'm basically saing is that I came across an example, that use an if statement in place of the ternary I put in there, I put in the ternary cause I wanted to, as I was trying to figure out the correct logic that should be apply to these ternary to work. I though of other option, as in switch statement, and else if... switch statement would win if this was a real application... really a switch statement in another function would win to refer to it... or better yet the || logic... Why did you write that... now I have to:
function checkSecretWord(guess){  var isCorrectGuess = guess;  var is = "";(isCorrectGuess === 'w3s') ? (is ='Correct, guess is w3s') : (isCorrectGuess === 'mdn') ? (is = 'Correct, guess is mdn') : (isCorrectGuess === 'w3c') ? (is = 'Correct, guess is w3c') : (is = 'Incorrect guess, try again');   return isCorrectGuess + " " + "is" + " " + is;} console.log(checkSecretWord('mdn'));  //logs 'mdn is Correct, guess is mdn'console.log(checkSecretWord('w3c'));  //logs 'w3c is Correct, guess is w3c'console.log(checkSecretWord('w3s'));  //logs 'w3s is Correct, guess is w3s'console.log(checkSecretWord('microsoft')) //logs 'microsoft is Incorrect guess, try again'
Please be careful... What I mean is, your bless if you are born with (refer to signature that will refer you to picture), that you are only born with that, or something doesn't manifest from that... Your lucky.I just see it as more practice. When I start writing code that I'm going to use for application; robust is the goal... The main goal is to get it to work, the goal after that is to make it as robust as possible. As someone said to me on this forum, I took that that to value and go by it as a philosophy to programming, and go by it.... No more, must get back to learning. BYE! Edited by L8V2L
Link to comment
Share on other sites

function checkSecretWord(guess){  var isCorrectGuess = guess;  var is = "";(isCorrectGuess === 'w3s') ? (is ='Correct, guess is w3s') : (isCorrectGuess === 'mdn') ? (is = 'Correct, guess is mdn') : (isCorrectGuess === 'w3c') ? (is = 'Correct, guess is w3c') : (is = 'Incorrect guess, try again');   return isCorrectGuess + " " + "is" + " " + is;}

I'm just bewildered...

  • Like 1
Link to comment
Share on other sites

function foo(foo, bar) {     return foo * bar; }var baz = myFunction.call(this, 10, 2);var foobar = foo.call(Object, 10, 2);console.log(foobar + "n" + baz);function bar(foo, bar) {     return foo * bar; }myArray = [10,2]; var foobaz = bar.apply(Array, [10, 2]);   // 	Will also return 20 var foo0 = bar.apply(Array, [10, 2]);   // 	Will also return 20 console.log(foobaz + "n" + foo0);/*Went over this in the 'js pocket reference' book, but I been seen apply, and call. I guess different transaltion is good. Still favorite the manaul, docuemnt, guide sltye. But this isn't really about the Function's method; call, and apply, this is about 'this'.*//*With call() or apply() you can set the value of this, and invoke a function as a new method of an existing object. <~~~~ So this make the function an method of an object? Please yes or no.*/
Link to comment
Share on other sites

I'm just bewildered...

:crazy: .... :huh: What is wrong with me experimenting? This is for practice and discovery. On that note:
//Original code: var add = (function () {    var counter = 0;    return function (x) {return counter+=x}})()add(10);add(15);document.getElementById("demo").innerHTML = add(20);//What I'm trying to do; there must be away to do it like this:function add(x) {         var counter = counter || 0;    function count(x) {return counter+=x;}   return(count(x));}add(2);add(2);
Link to comment
Share on other sites

No.

With call() or apply() you can set the value of this, and invoke a function as a new method of an existing object. <~~~ I copy and paste this off w3s tutorials function section; Invocation page. If no then what does it mean/saying?
Link to comment
Share on other sites

With call() or apply() you can set the value of this, and invoke a function as a new method of an existing object.

Note the word "as". It is a simile. It executes the function as if it were a method of the object. It does not actually make it a method of the object.I doubt you'll understand this, because you seem to get into things you don't understand yet, but specifically what it does is execute the function in the scope of an object.
  • Like 1
Link to comment
Share on other sites

Note the word "as". It is a simile. It executes the function as if it were a method of the object. It does not actually make it a method of the object.I doubt you'll understand this, because you seem to get into things you don't understand yet, but specifically what it does is execute the function in the scope of an object.

I understand. It act as an Object's, or in apply case, as an Array's method. Thank you, if I have read more carefully, I would have notice that... probably not... I don't know. Edited by L8V2L
Link to comment
Share on other sites

Why must there be a way? In order to do that you need a static variable, and Javascript doesn't have static variables.

Because it's magic JSG! YOU...(turn away hanging my head, with sorrow in my eyes) you just don't understand. Because... Because it's magic. (turn back toward you, with strength and determination in my eyes) And I will never give up!Pokémon first season theme song instrumental playing:I wanna be the very best,Like no programmer ever was.To learn JavaScript is my real quest,To master it is my cause.I will read over the materials,Searching for more and broaden my mind.Each concept to understandThe power that is codeJavaScript, (gotta learn it all) its you and meI know its my destinyJavaScript, oh, you're my best friendIn a application we must defendJavaScript, (gotta learn it all) a heart so trueOur courage will pull us throughYou teach me and I'll teach youJa-va-Script, gotta learn it allEvery material along the wayWith courage I will faceI will learn every dayTo master the languageCome with me, the time is rightThere's no better teamArm in arm we'll learn the languageIt's always been our dreamJavaScript!(Gotta learn it all)It's you and meI know it's my destinyJavaScript!Oh, you're my best friend,In a application we must defend.JavaScript!A heart so true.Our courage will pull us through.You teach me and I'll teach you.JavaScript!(Gotta learn it all)x5Yeah!JavaScript!It's you and meI know it's my destinyJavaScript!Oh, you're my best friend,In a application we must defend.JavaScript!A heart so true.Our courage will pull us through.You teach me and I'll teach you.JAVASCRIPT! Edited by L8V2L
Link to comment
Share on other sites

Yeah, I didn't read any of that. You said something about not liking it when authors try to inject humor or personal stories or go off topic, right?

... but, that's how I feel...
Link to comment
Share on other sites

  • 2 weeks later...

How to trigger two function in an statement; refer two both, the for statement out side the closure functions and the switch statement:

<!DOCTYPE html><html><body><p id="demo"></p><script>var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ", str = "", i = 0, txtLength = txt.length, p = document.getElementById("demo");fo:function funcFor(){for(i, txtLength; i < txtLength; i++){p.innerHTML+=txt[i].toLowerCase();}}whil:function funcWhile(){while(i < txtLength){document.getElementById("demo").innerHTML += txt[i];i++;}}if(0<1){//whil://funcFor() /*with(*/eval(funcWhile()&&funFor());/*); funcFor();*/}/*switch(0){case 0 : {funcFor();funcWhile();//break;}case 1:{funcWhile();break;}}*/</script></body></html>

 

 

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"/><title>Demo01</title><style>#demo{color:#9f9}</style><script>var txt; //global//init function set/get data needed after onload event take placefunction init() { var button = document.getElementById("btn1");button.onclick = toggletxt;txt = document.getElementById("demo").innerHTML;}//end of init function//learnt: can have function out side of init functionfunction toggletxt(){var p = document.getElementById("demo");p.innerHTML=(p.innerHTML == txt)?("Hello JavaScript"):(txt);}//end of functionwindow.onload = init;</script></head><body><h1>My First JavaScript</h1><p>JavaScript can change the content of an HTML element:</p><!--Original problem: Could not set toggletext in element i.e: onclick="toggletext()";--><button type="button"; onclick="toggletext()";>Click Me!</button><!--Unless the actually code is set underneath the button element/node/tag--><!--<button id="btn1">Click Me!</button>--><p id="demo">This is a demonstration.</p><p>Try to give variables and functions meaningful names.</p><p>Don't try to walk the DOM without considering text and comment nodes.</p></body></html>

 

Labelled statements are only used in conjunction with labelled break and continue statements. ECMAScript has no goto statement.reference from ECMAscript...control structure... could be call a control structure expression??? control structure statement... But as he said, control structure... a new <~~~~ technic for me.... but as stated, not usefully... but still a technique that could be use... keep in mind... control strucute.... control structure..can some one give me an example of the with statement?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...