Answers
Answers
2 . Please follow the plunker link and provide a written *code review* for the sample code
https://plnkr.co/edit/FZH2isUyd2n0JsWZ?open=lib%2Fscript.js
- The code aims to create a sentence about favorite fruits and show it on a webpage.
2. Issues Found:
-Variable Declaration: The code doesn’t consistently declare variables.
- Errors: There’s a strange line (`<> X`) that doesn’t belong and some incorrect ways of
combining strings.
- Function Syntax: The function that creates and displays the sentence isn’t properly
closed.
2. Improved Code:
```javascript
Var first_part = ‘I like to eat’;
Var myStr = ‘’;
Function GetSentence() {
Console.log(‘starting function’);
myStr = first_part;
$(‘body’).find(‘h1’).text(myStr);
Console.log(myStr);
}
GetSentence();
```
4. What Changed:
- Consistent Variable Declaration:** Added `var` to ensure `first_part` is declared
correctly.
- Fixed Errors:Removed unnecessary lines and corrected how the sentence is built.
- Function Closure: Added the missing end for the function to make sure it works
properly.
5. Additional Suggestions:
- Error Checking: Make sure the fruit list is valid.
- Better Comments:Add clearer comments to explain what the code does.
- Modern JavaScript: Consider using newer features for cleaner code.
With these fixes, the code will correctly build the sentence and display it on the webpage.
3 See https://docs.google.com/document/d/1q-
5lE5jEvMHj95SfhQlJzk_7q3MpQbzOGQ7IsXQUAec/edit?usp=sharing. What is wrong with the
function computeTotal()?
The Issue with the `computeTotal` function is in the way the `each` method is being used.
Specifically, the `this` keyword inside the `each` function does not refer to the current list item
(`<li>`) as expected. Instead, it refers to the global context because an arrow function (`() => {}`)
is being used. Arrow functions do not have their own `this` binding; they inherit `this` from the
parent scope, which is not the desired behavior in this case.
To fix this, you should use a regular function (`function() {}`) inside the `each` method. Here’s the
corrected version of `computeTotal`:
```javascript
Function computeTotal() {
Let total = 0;
Console.log($(“li”));
$(“li”).each(function() {
});
$(“#total”).text(`Total: ${total}`);
Document.addEventListener(“DOMContentLoaded”, function(){
computeTotal();
});
4 See
https://docs.google.com/document/d/1oYl1czN2KySsh47KrXOmenyxvEhzaWWljFAMb6T2OJU/
edit?usp=sharing. What is the order of the items in the displayed list? Explain.
A deadlock is a situation where a set of processes get stuck because each one is waiting for a
resource held by another, creating a cycle of dependencies that halts progress. It occurs due to
mutual exclusion, hold and wait, no preemption, and circular wait conditions. The effects include
system hang, performance degradation, resource wastage, and potential data loss.
6 Write a piece of code for the following problem and analyze its complexity: Given an
array of distinct integers and a target sum, find two numbers in the array that add up to
the target sum.
Seen = set()
If complement in seen:
Seen.add(num)
Return None
7 You are on a team of 3 people, and one of your teammates continuously lags behind in
the number of completed tasks. How would you handle this?
One notable role model in technology is **Linus Torvalds**. He is the creator of the Linux
operating system, which has become a cornerstone of modern computing, powering everything
from servers and smartphones to embedded systems. His contributions have had a profound
impact on open-source software, promoting collaboration and innovation across the tech industry.
Torvalds is admired for his technical expertise, leadership in the open-source community, and his
commitment to developing robust and scalable software solutions.
9 What is your current compensation structure and how do you see yourself in the next 5
years?
I am fresher here and In next 5 years I see my self as an important part of your organization.
10 If we discover that we are a good match together – when would you be available to join
our team?