Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NW-6 | AREEB-SATTAR | JS1| [TECH ED] Complete week 4 exercises | WEEK-4 #184

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

areebsattar
Copy link
Contributor

Learners, PR Template

Self checklist

  • I have committed my files one by one, on purpose, and for a reason
  • I have titled my PR with COHORT_NAME | FIRST_NAME LAST_NAME | REPO_NAME | WEEK
  • I have tested my changes
  • My changes follow the style guide
  • My changes meet the requirements of this task

Changelist

Briefly explain your PR.
Completed week-4 exercise except stretch

Questions

Ask any questions you have for your reviewer.

expect(repeatString("Ali", 0)).toBe("");
expect(repeatString("Ali", 1)).toBe("Ali");
expect(repeatString("Ali", -2)).toBe("negative count is not valid");
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a well written code.

Copy link

@HadikaMalik HadikaMalik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code make sense and is well written. In some parts I found that it can be more concise but still it makes sense. Well done!

function countChar(str, char) {
let index = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === char) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a well written code however can you explain this a bit? It seems a bit confusing to me, maybe if I understand what you have done here, it would make sense to me.

function getOrdinalNumber(num) {
if (num % 10 === 1) {
if (num === 11) {
return "11th";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did mine a bit differently. Mine is a bit more concise, but this makes sense as well. Well done!

@@ -1,5 +1,24 @@
// implement a function countChar that counts the number of times a character occurs in a string

function countChar(str, char) {
let index = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you think of a more descriptive name for this variable? We already have i which keeps track of the index

if (str[i] === char) {
index++;
}
while (str[i + 1] === char) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my understanding, this loop will keep counting up and moving through the string if the next element in the string is the character we're counting. However if we just let the for loop run, it will already check every character in the string. So this while loop isn't doing much for us, just using the for loop will be easier to understand 🙂

@@ -1,4 +1,18 @@
// In this week's prep, we started implementing getOrdinalNumber
function getOrdinalNumber(num) {
if (num % 10 === 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great use of the modulo % operator Areeb!

if (num != Math.round(num)) {
return false;
}
for (var i = 2; i < num; i++){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great spot that you can begin this loop at 2! I would just replace var with let, as this helps to keep the variable scope as small as possible, in this case, only inside the for loop. Otherwise you would be able to access i outside of the loop which could cause bugs.

Comment on lines +34 to +39
if (num === 0) {
return "";
}
if (num === 1) {
return str;
} else return repeatedString;
Copy link
Member

@JayMayer JayMayer Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check what happens when you give str.repeat(..) the number 0 or 1. You might find that you can remove a few if statements from your function 😉

As a general rule of thumb, even if you don't need to repeat the string (if the number is 1, for example), as long as the built-in function still behaves as expected, it's fine to still use its output.

Copy link
Member

@JayMayer JayMayer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good Areeb, you're able to really showcase your JavaScript and algorithm knowledge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants