elmira202003 / hyf-javascript2 Goto Github PK
View Code? Open in Web Editor NEWHYF JavaScript Module 2
HYF JavaScript Module 2
Hi Elmira, here is my feedback on your homework. Overall, well done!
OK
I noticed you copied my solution. No point in me reviewing my own code. Therefore, I took your code from an earlier commit:
let sayNumber = [];
function sayThree() {
sayNumber.push('sayThree');
}
function sayFive() {
sayNumber.push('sayFive');
}
function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
let numberArr = [];
for (let i = startIndex; i <= stopIndex ; i++){
numberArr.push(i);
if (i % 3 === 0) {
sayThree();
}
if (i % 5 === 0) {
sayFive();
}
}
console.log('Thee original array is : ' + numberArr);
console.log(sayNumber);
}
threeFive(10, 15, sayThree, sayFive);
I would have been happy with your original, own solution: it was close enough.
As we explained in the last lecture, it is better to design your functions for reusability. In this case, it would be better if your repeatStringNumTimes
would simply return the new string without console logging it. Then you can console.log it when you call your function, like so:
function repeatStringNumTimes(str, num) {
str = num < 0 ? "" : str;
let newString = "";
for (let i = 0; i < num; i++) {
newString += str;
}
return newString;
}
console.log('written with FOR LOOP: ' + repeatStringNumTimes("abc", 3));
Now you can even do something like this:
console.log('written with FOR LOOP: ' + repeatStringNumTimes(repeatStringNumTimes("abc", 3), 3));
OK
freeCodeCamp uses var
instead of let
and const
. Just remember, if you have a choice then use let
and const
.
You can use the short-cut form of product *= arr[i][j]
.
You can solve the n-dimensions problem with a recursive function (google for it). See the solution I posted in slack.
Your conclusions are correct.
OK
This is a bit of bismillah code.
'use strict';
let mainArr = ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c'];
mainArr.getUnique = function () {
let o = {}, uniqArr = [], mainArrItem;
for (let i = 0; mainArrItem = this[i]; i++) {
o[mainArrItem] = 1
};
for (mainArrItem in o) {
uniqArr.push(mainArrItem)
};
return uniqArr;
}
console.log(mainArr.getUnique());
I had to run it in the debugger to see what is happening. First of all, the loop condition in your first for
loop is actually an assignment (because of the single =
). So this loop iterates until i
is equal to the length of mainArray
. After that this[i]
becomes undefined
which causes this loop to terminate.
I can see that you are exploiting the fact that object keys are unique. A simpler way would be to use an ES6 Set()
.
The next thing I saw is that you add a method to an existing array. Perhaps this was just an experiment, but for real code this just unnecessarily complicates the code.
Hi Elmira,
Very well done dear, your code looks neat and clean, and it is working. I only have one comment:
In line No. 7, you can remove the newNumber = newNumber.map.....
. Instead, you can just do it in one line as following:
newNumber = number.filter(arrNumbers => (arrNumbers % 2 !== 0)).map( doubledNumbers => ( doubledNumbers * 2));
This gives you the same result.
Wish you a lovely evening!
Dear Elmira
you do a great job and and clean code, but I realy I do't understand the code in bonus.js maybe can you explain it to me later.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.