What is the best way to determine the P/E ratio for a specific stock?

Need help with a formula calculation. I have the value for net worth, but I am having trouble iterating over the EPS values and multiplying them by the shares held. Can anyone suggest a solution? Thank you! You can find the Plunker here

        <div>
            <h3>P/E Ratio: {{getNetWorth())}}
        </div>

Here is the script snippet:

$http.get('data.json').then(function(response) {
     $scope.stocksArray = [];
     var indexes = [];
     var epsindex = [];
     var eps = {};
     $scope.eps = response.data.eps;

Answer №1

Although it may not receive any accolades, you can find the essence of the issue here.

Your json file has an unusual structure:

  -price
    --stock-name:stock-price
  -eps
    --stock-name:stock-eps

The structure should be like this:

  -stock-name
    --price:actual-price
    --eps:actual-eps

This adjustment would allow you to import it as objects without having to manually instantiate the entire collection. I worked with the given data and it functions correctly.

The crucial part is in the next two lines:

$scope.eps = response.data.eps;

AND

mystock.eps = $scope.eps[key];

By including the eps collection as a separate array, I was able to incorporate it as a field in the object property.

Implementing getPortfolioPE() was straightforward:

$scope.getPortfolioPE = function(){
          let temp = 0;
          let total = 0;
          for(let i = 0; i < $scope.stocksArray.length; i++){
            total += $scope.stocksArray[i].eps * $scope.stocksArray[i].shares
          }
          temp += $scope.getNetWorth()/ total;
          return temp.toFixed(2);

        }

Answer №2

In my opinion, the $http.get function should be set up to retrieve an array of stocks where the PE ratio is a property of each individual stock.

Therefore, the controller code should look something like this:

$scope.stocks = response.data;

Additionally, in the template, you will likely need to use ng-repeat to iterate through the array of stocks and display the PE ratios:

<li ng-repeat="item in stocks">
    Your PE ratio is: {{item.pe}}
</li>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Making a Cross-Origin Resource Sharing (CORS) request with jQuery utilizing the $

I am currently in the process of building a web application that relies heavily on data from multiple domains. Approximately 90% of the requests made by my application are cross-domain requests. However, I have encountered an issue where I am unable to re ...

What could be causing the failure to retrieve the salt and hash values from the database in NodeJS?

My current issue involves the retrieval of hash and salt values from the database. Although these values are being stored during sign up, they are not being retrieved when needed by the application. Below, you will find snapshots of the database, console s ...

Mistakes encountered when compiling TypeScript Definition Files

I am looking to convert my JavaScript files (*.js) to TypeScript files (*.ts) in my ASP.net MVC5 application (not an Asp.net Core app). I am using Visual Studio 2015. After downloading the TypeScript Definition Files into the Scripts\typings\ fol ...

What is the best way to allow all authenticated routes to display the Devise ajax Login form?

I have successfully incorporated Devise to accept Ajax Login and have designed a form for logging in via ajax, displayed within a modal. However, I am only able to view the form and login when I set up event binders for specific buttons that activate the m ...

Leveraging environmental variables in a Vue.js web application

The article I recently came across discussed how to effectively utilize environment variables in vuejs. Following the instructions, I set up my local .env.local file and also installed dotenv. VUE_APP_AUTH_AUTHORITY = 'http://localhost/auth' I ...

Uncovering secret divs with the power of jQuery timing upon reload

Currently, I am in the process of developing a custom Wordpress theme for my blog which includes an overlay-container. When a button is clicked, this container slides in from the top and pushes down the entire page. To achieve this functionality, I am uti ...

Associating a variable with a nested scope in AngularJS

I am currently working on a directive that generates a row within a table. To populate the table with this directive row, I am using ng-repeat to loop through a list of items, like so: <tr ng-repeat='item in items' rowItem='item' /& ...

What is the best way to duplicate a text using a button in ReactJS?

Hey there, I'm working with an array and trying to figure out how to copy the text in a p element ({item.adress} => this is part of an array item) when a button is clicked. Could you lend me a hand? import classes from './CryptoBox.module.css& ...

Ensure the smooth scrolling feature is activated by adding an active class when either clicking or manually scrolling the

I have a script that enables smooth page scrolling, but I want it to automatically add an "active" class to the link corresponding to the section currently in view. While there are similar solutions out there, most of them only apply the class when the lin ...

Associate information with HTML elements

I've come across this question multiple times, but the solutions are mostly focused on HTML5. My DOCTYPE declaration is: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> I'm looking t ...

Hold off on running addThis

My website utilizes Google Maps along with my own JavaScript functions. Additionally, I am integrating the AddThis JavaScript for social sharing buttons. For optimal performance, I need both my custom JavaScript and Google's JavaScript to load and exe ...

Sharing information between sibling modules

Currently, I am faced with the challenge of transmitting data between two sibling components within the following component structure. The goal is to pass data without changing the relationships between these components. I prefer not to alter the componen ...

What is the best way to keep calling an AJAX function until it receives a response from a previous AJAX call

I am looking to continuously call my ajax function until the previous ajax call receives a response. Currently, the page is loading every second and triggering the ajax function, but I want it to keep calling the ajax function until the previous one has c ...

Troubleshooting a JavaScript Error in AngularJS Module

I created a Module called "app" with some helper functions in the file "scripts/apps.js": angular.module('app', ['ngResource']).run(function($scope){ $scope.UTIL = { setup_pod_variables: function (pods){...} ... }); Now, I want to ...

Tips on traversing a JSON array in AngularJS using the ng-repeat directive

My service returns the following JSON data: {"categories":[{"category_id":"20","name":"Desktops"}, {"category_id":"18","name":"Laptops &amp;"},{"category_id":"25","name":"Components"}, {"category_id":"57","name":"Tablets"}, {"category_id":"17","name": ...

Building connections in parse.com using various classes (JavaScript SDK)

Currently using parse.com in conjunction with the JavaScript SDK. Despite my extensive research, I have been unable to find any examples that directly address the issue I am facing. Here is the specific goal I am striving to accomplish: I aim to establi ...

The transformation rotation applied in CSS must not have any impact on the styling of my span and paragraph

Snippet: .details-section{ background-color: rgb(83, 83, 83); height: 200px; } .icon-container{ border: 2px solid #c49b63; width: 90px; height: 90px; transition: 0.5s ease; } .box i{ font-size: 60px; color: black; margin-top: 13px ...

Actions for jQuery's mouseenter and mouseleave events

I've implemented a jQuery script that controls the visibility of elements based on mouse events: $("#divid").mouseenter(function() { $('#divid').show(1000); }).mouseleave(function() { $('#divid').hide(1000); }); $("#hldiv" ...

The Google Drive API in Node.js is notifying the deletion of files

I encountered an issue with the Google Drive API in my application. Even after deleting files from Google Drive, the listfiles function still returns those deleted files. Is there a solution to prevent this from happening? Below is the function of my API: ...

The compatibility between Babel 7 and the preset-es2015 is not very reliable

After reading this useful tutorial on implementing server-side rendering with create-react-app, I attempted to execute the following code snippet: require('ignore-styles'); require('babel-register')({ ignore: [/(node_modules)/], ...