Playing In The Sandbox
One of the best ways to practice your coding skills is by playing in the sandbox, repl.it or even irb. I’ve been practicing some basic JS code for a couple days. Using the console to see real time changes vs. coding blindly definitely helps. Having “let val” will allow the value change without added work.
let val;//Number to stringval = String(777);val = String(1 + 1);
//Bool to stringval = String(true);
//Date to Stringval = String(new Date());
//Array to Stringval = String([1, 2, 3, 4]);
//toString()val = (5).toString();
//String to a Number orval = Number("5");val = Number(true);val = Number(false);val = Number(null);val = Number("hello"); //Nan not a numberval = Number([1, 2, 3, 4]);val = parseInt("100.30"); //whole numberval = parseFloat("100.30"); //to show decimal//Outputconsole.log(val);console.log(typeof val);//console.log(val.length);
console.log(val.toFixed()); //to show decimal(parseFloat)you can put 2 inside toFixed(2) to show 2 decimal placesconst val1 = String(5); //will concatnate '5' as a string + 6 = 56const val2 = 6;const sum = val1 + val2;console.log(sum);console.log(typeof sum);
Playing around with Math & Numbers
Being a Software Engineer requires you to have some knowledge with numbers, so playing around with numbers will do you some good. Know where to go to find those handy methods.
const num1 = 150;const num2 = 50;let val;//simple math w/numbersval = num1 + num2;val = num1 * num2;val = num1 - num2;val = num1 / num2;val = num1 % num2;//Math Object (has props(attribute) & methods)val = Math.PI;val = Math.E;val = Math.round(3.8); //round a number this ex. to 4val = Math.ceil(3.4); //rounds to 3 .floor does sameval = Math.sqrt(64); //gives us 8 64 squaredval = Math.pow(8, 2); //8 the the 2nd power of 2val = Math.min(2, 33, 4, 1, 55, -1); //gives min -1val = Math.max(2, 33, 54);val = Math.random();val = Math.random() * 20; //gives random # w/ decimal bet 0-19 to get a whole number wrap in Math.floorval = Math.floor(Math.random() * 20 + 1);console.log(val);
Learning about Strings are also a great way to exercise your skills
I’m not one of those natural coders by any means. For those who are, you’re amazing. I know these concepts are considered basic. You gain a lot of knowledge by pairing up and working on projects. However, any little bit helps.
//Stringsconst firstName = "William";const lastName = "Alexander";const age = 36;const str = "hello there my name";let val;//Concatenationval = firstName + " " + lastName;//Appending add on to a variable not replaceval = "Krystal";val = "Kira";val += "Kira";val = "Hello, my name is " + firstName + "and I am" + age;// which will print out Hello, my name is Brad and I am whatever age is.//Escapingval = "That's awesome, I can't wait.";//escape with a '\'//escapes the quote withval = "That's awesome, I can't wait";//lengthval = firstName.length;//concat()val = firstName.concat("", lastName);//Change case toval = firstName.toUpperCase();val = firstName.toLowerCase();val = firstName; //taking 0 index from this value which would give us first Letter of the firstName//indexOf()val = firstName.indexOf("l"); //looks for first 'l' it finds and gives the index of that letterval = firstName.lastIndexOf("l"); //gives us the last 'l' giving us the index//last charac. of a string but it could change so create a valval = firstName.charAt("2");//get last charval = fristName.charAt(firstName.length - 1);//substring()val = fristName.substring(0, 4);//gives us the first 4 letters of 'william' which is will//split//const str = 'hello there my name is'//val = str.split(' ');//will give you an array [hello, there, etc.]//includes() gives T or F valuesval = str.includes("foo");console.log(val); //so we can overwrite the values
“Little Success will get you to your goals. One code at a time.”