Explore the power of using Math.pow in JavaScript with UIWebView in iOS

Seeking guidance on how to evaluate a JavaScript string within iOS using the code snippet:

[self.webView stringByEvaluatingJavaScriptFromString:formula]

The current code in question:

NSString *formula = @"( 1.5 * Math.pow(2, 3) ) / 12";
formula = [NSString stringWithFormat:@"eval('%@')", formula];
NSInteger value = [[self.webView stringByEvaluatingJavaScriptFromString:formula] integerValue];

Encountering an issue with the Math.pow function - when included in the string, it returns an empty result instead of a numeric one from the webView output. In need of assistance to modify the code to successfully handle strings containing Math.pow.

Answer №1

Here is where the issue lies:

NSInteger result = [[self.webView stringByEvaluatingJavaScriptFromString:equation] integerValue];

By converting your output to an integer value, it ends up getting rounded off.

The correct approach would be:

float result = [[self.webView stringByEvaluatingJavaScriptFromString:equation] floatValue];

Note the change in data type from result and using floatValue for the string conversion.

Also, just a heads up, you don't necessarily need the eval call.

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

Using CSS to position an element relative/absolute within text inline

Need help aligning caret icons next to dynamically populated text in a navbar menu with dropdown tabs at any viewport size. Referring to positioning similar to the green carets shown here: https://i.stack.imgur.com/4XM7x.png Check out the code snippet bel ...

Tips for embedding a script into an HTML document

I've been experimenting with tinymce npm and following their guide, but I've hit a roadblock. Including this line of code in the <head> of your HTML page is crucial: <script src="/path/to/tinymce.min.js"></script>. So, I place ...

Slider Navigation Issue

I encountered an issue. * Encountered exception 'NSInvalidArgumentException', message: * While trying to follow a tutorial on creating a slider menu at , I am running into an error. I have followed all the steps correctly, but for some reaso ...

Troubleshooting the issue: Difficulty in activating Navbar dropdown using Angular and ui-router

I've been struggling with this issue for some time now - trying to make a simple bootstrap navbar dropdown function properly with angular ui-router and ui-bootstrap (1.3.3, the latest version). Below is my current code: <div class="container"> ...

Can you verify that the client's date and time locale are accurate in JavaScript before proceeding with processing?

For my application, I am seeking the current locale datetime. However, before retrieving the date, it is crucial to verify that the local date and time are accurate. If the user has adjusted the date and time incorrectly on their machine, I must prompt a ...

Can JavaScript be used to dynamically assign events to elements on a webpage?

I am currently using the following code: if ( $.support.touch == true ) { $(window).on('orientationchange', function(event){ if ( full == false ) { self.hideAllPanels("7"); } }); } else { $(window).on(&apo ...

The viewport width in NextJS does not extend across the entire screen on mobile devices

I'm currently tackling a challenge with my NextJS Website project. It's the first time this issue has arisen for me. Typically, I set the body width to 100% or 100vw and everything works smoothly. However, upon switching to a mobile device, I not ...

What are the steps to execute a file on localhost using an HTML page?

Currently, I have an HTML file that leverages the Google Calendar API javascript to read the next event on my calendar. The functionality works flawlessly when manually inserting a specific URL in a browser address window, which triggers a GET request in a ...

Maintain the values of radio buttons, dropdowns, and checkboxes using Javascript

When I click on a radio button, it activates a drop down menu. Upon selecting different values from the drop down, various checkboxes become visible. Now, I want to preserve the selection of the radio button along with the selected drop down values and ch ...

jQuery code not being triggered on secondary page

I'm currently working on a website project for one of my university courses (Link: ) and I'm encountering an issue that I could use some help with. The problem involves a "back-to-top" button that uses jQuery to smoothly scroll the page upwards. ...

What is the best method for establishing a page location during rendering/onload?

It seems like there is a JavaScript event happening here using the onload attribute. But for the life of me, I can't seem to crack this code. <body onload="moveToHere('reference')"> I'm stuck and could really use some assistance ...

The type 'undefined' cannot be assigned to the type 'string | Buffer | { key: string | Buffer; passphrase: string; } | GetPublicKeyOrSecret'

Verification Code for JWT This function is used to verify a jwt using typescript. public verify( token: string, secretOrPublicKey?: string | Buffer, options?: jwt.VerifyOptions ): Promise<object | string> { if (!secretOrPublicKey) { ...

Is it necessary to download all npm packages when starting a new project?

I recently started learning about npm packages and node. I noticed that when installing packages, they create numerous folders in the "node modules" directory. This got me thinking - when starting a new project, do I need to re-install all these packages ...

Mastering the use of node cluster mode in conjunction with agenda cronjobs

Currently, I am utilizing nodejs and agenda to run cronjobs. With a total of 10 cronjobs in place, the process is taking longer as nodejs is single-threaded and they all run simultaneously. In an attempt to resolve this issue, I experimented with pm2 by e ...

How to retrieve all the text inside an element using Protractor and Selenium

<div class="test"> <span> <div>CONTENT </div> </span> <div> <ul> <li> <span>More text</span> EXAMPLE1</li> <li>EXAMPLE2</li> ...

Tips for refreshing the <img> tag using a user image

I am currently designing a bootstrap webpage and trying to implement a feature where the user can input an image, which will then be displayed in a preview modal along with some text provided by the user. The modal is functioning correctly. Here is the co ...

Obtain the SSID from the access point on an iOS device

I am seeking unique information from access points, specifically the MAC address (BSSID). I have attempted a code snippet, but it only seems to work when I am connected to a network. - (id)fetchBSSIDInfo { NSArray *interfaces = (id)CNCopySupportedInterfac ...

Setting up a service in angularjs that is reliant on another service

One thing I'm trying to figure out is how to utilize a service like $http outside of the $get function. Is this even possible? Currently, my code loads a json file that contains a dictionary used by my application in various ways. I want users to have ...

I would like to find the difference between two arrays by creating a third array that only contains the elements not found in the first array

Subtracting elements from two arrays. arr1=["AT","IN","UK","US"] and arr2=["UK","ZA"]. I want the result to be arr1-arr2 = ["AT","IN","US"] I have attempted to perform the subtraction of the arrays as described above, but instead of obtaining the correct ...

Encountering an unspecified array type when making an Ajax request with JSON

Currently, I am developing a web application that allows users to play sudoku with a Java back-end. To communicate with my jQuery using Ajax, I have created a servlet. Upon sending the generated array (the sudoku) to my web application through this servle ...